               IUIComponent(window).document == null)
               IUIComponent(window).document = IUIComponent(parent).document;
 
-        if (parent is IUIComponent && IUIComponent(parent).document is IFlexModule &&
-              window is UIComponent && UIComponent(window).moduleFactory == null)
-              UIComponent(window).moduleFactory = IFlexModule(IUIComponent(parent).document).moduleFactory;
-
+        if (window is IFlexModule && IFlexModule(window).moduleFactory == null)
+        {
+            if (moduleFactory) 
+                IFlexModule(window).moduleFactory = moduleFactory;
+            else if (parent is IUIComponent && IUIComponent(parent).document is IFlexModule)
+                IFlexModule(window).moduleFactory = IFlexModule(IUIComponent(parent).document).moduleFactory;
+        }
+        
         var sm:ISystemManager = getTopLevelSystemManager(parent);
         var children:IChildList;
         var topMost:Boolean;

import mx.core.IChildList;
import mx.core.IFlexDisplayObject;
import mx.core.IFlexModule;
import mx.core.IInvalidating;
import mx.core.ISWFLoader;
import mx.core.IUIComponent;

    public function addPopUp(window:IFlexDisplayObject,
                             parent:DisplayObject,
                             modal:Boolean = false,
                             childList:String = null,
                             moduleFactory:IFlexModuleFactory = null):void
    {
        // trace("POPUP: window is " + window);
        // All popups go on the local root.
