API logic sorted