refactoring, move some code from controller to model