Extract query parsing logic into