refactor logout functionality