Added query validation and type autodetection