Refactoring range query, working