Refactored code from controller to service