moved code from view to model