Fixed race conditions potentially causing deadlock.