Refactor flatten transform to use Visitor pattern.