Query generation now seems to work