Solving thread nesting problem