Refactored logout method