Moving logic from view to controller