Fixed race condition bug