move code from controller to model