Improved thread handling.