API code refactor