Implemented clean shutdown functionality.