Clean shutdown logic and add a SIGHUP handler.