Refactor ProcessQuery out