Fixes race condition bug