Major improvements to the multithread management code.