Threads: Added some locking here and there.