Fixing race condition bug