tidy up of api query implementations and small optimizations