// pretty dirty, especially regarding cleanup of main classes
svoid dm_replaceModuleInstance(DynModule newInstance) {
  assertNotNull(newInstance);
  O stem = dm_stem_mandatory();
  O oldInstance = get(stem, "o");
  if (oldInstance != newInstance) {
    print("Replacing instance for module " + dm_moduleID() + ": "
      + oldInstance + " => " + newInstance);
    set(stem, o := newInstance);
    
    // Revisualize because UI often has pointers into the module instance
    dm_revisualize();
  }
}