Add thread safety to some classes

Probably overdid the locking...hopefully I didn't make the problem worse
in the opposite way!