Refactored logout mechanism.