Small fixes
Fixed deadlocks and assorted bugs.