Refactored checkAuth function heavily.