refactor controller code