Fixed test and race condition