Added a mechanism for shutting down with a timeout.