Moved API layer query code into API from core