Investigate using a QueryVisitor rather than using a for loop.