Moved logic from view to controller