refactor API controller code