Fixed a bug: using length instead of nrow. Convert query to stringw