Refactor main_shutdown