Various improvements for multithreading