         if (!layer)
             return new Vector.<IVisualElement>(0);
         
-        // TBD(hmuller): if the factory for any of the oldVisible elements isn't 
-        // the specified factory (e.g. because factory is null) then 
-        // removeGridElement all of them.  Maybe add removeGridElements(factory).
-        // Replace the following:
-        
+        // If a factory changed, discard the old visual elements.
+        
+        if (discardGridElementsIfFactoryChanged(factory, layer, oldVisibleElements))
+            oldVisibleIndices.length = 0;
+                       
         if (factory == null)
-        {
-            for each (var oldElt:IVisualElement in oldVisibleElements)
-                layer.removeElement(oldElt);
-
             return new Vector.<IVisualElement>(0);
-        }
-        
+
         // Free and clear oldVisibleElements that are no long visible
         
         freeLinearElements(oldVisibleElements, oldVisibleIndices, newVisibleIndices, lastIndex);

        
        const newVisibleElementCount:uint = newVisibleIndices.length;
        const newVisibleElements:Vector.<IVisualElement> = new Vector.<IVisualElement>(newVisibleElementCount);
        
        for (var index:int = 0; index < newVisibleElementCount; index++) 
        {
            var newEltIndex:int = newVisibleIndices[index];

        newVisibleRowIndices:Vector.<int>, newVisibleColumnIndices:Vector.<int>,
        layoutFunction:Function):Vector.<IVisualElement>
    {
        if (!layer)
            return new Vector.<IVisualElement>(0);

        // If a factory changed, discard the old visual elements.
        
        if (discardGridElementsIfFactoryChanged(factory, layer, oldVisibleElements))
        {
            oldVisibleRowIndices.length = 0;
            oldVisibleColumnIndices.length = 0;
        }

        if (factory == null)
            return new Vector.<IVisualElement>(0);
        
        // Create, layout, and return newVisibleElements
        
