Fixed a slew of thread safety bugs