moved some code from controller to model