fix query formation