Refactor the Query implemetation