Fixed some deadlocks and const-correctness issues