Fixed some bugs that caused deadlocks.