PEP* Cleanups and added timeouts for threads when shutting down or restarting.