API code refactored