Fixed some race conditions with mutexes.