query processing refactored