Completed login/logout function