More cleanup. Refactored query interface. Issue #473