Move code from view into controller