refactored quit handling