Bug/query refactor