refactoring API code