Fix another race condition. This code is getting ugly :(