move code from view to controller