refactor API code