Moved code from view to model