Deadlock fix and cleanups