Fix race conditions