Fix #170:  handle namespaces in queries

Review this PR *after* #254 is merged (it is based from that branch).