Fix race condition bug