Query test improvements & minor refactoring

As long as I'm not missing anything, it would be reasonable to add a check here whether min <= max and throw an exception if not eventually.