Fixed possible deadlocks.