Fixes a race condition bug