Refactored code out of auth service.