Refactored the way shutdown hooks were handled