Properly handle quit events