Refactor handle_shutdown to wrapper error handling function