Refactoring API handler