Fix thread sync issues