refactor code from controller to model