Refactor controller code