refactored some query parsing code