Improved logout functionality