Multithreading fix and simple cleanups