refactor query system