Fix query & range-where. Add some tests