fixed and refactored logout functionality