move query building to library