Extract query building into a separate class