Fixes and tests for query