Fix for query construction