thread-safety: Finished reviewing the code for thread unsafe spots. Added some more locks.