refactor quitting functionality