Refactor: reorganize controller code