Fixed race conditions (I hope...)