Improved API endpoints logic