Fixed race condition logic