Solving thread sync issues