Moving code to controller