code improvements for query construction