move code from controller to services