refactor code on main controller