Added support for select fields, simplify query logic