Move code into controller