Added query validation.  Moved query-normalization logic to own namespace.