Various fixes in the multithreading code.