Move the exit procedure into its own function