Fix some bugs in querying interface

Eric, just found a bug and fixed it for last updated querying interface.
I will continue to test the code for a period. After the interface is stable, I will write a short article on the wiki to explain the usage.