Tidied shutdown logic.