Logout functionality and redirect loop fix