Refactor query classes