diff --git a/html/main.php b/html/main.php
index b24011e8692269a1f91c5e9e1a3a995c0050f4a9..daef0db78bb2ad1d5f34586f9a54c24706acec73 100644
--- a/html/main.php
+++ b/html/main.php
@@ -97,11 +97,11 @@ Language::init();
 /* Prepare plugin list */
 pluglist::load();
 
-/* Check Plugin variable */
-if (session::global_is_set('plugin_dir')) {
-  $old_plugin_dir = session::global_get('plugin_dir');
+/* Check previous plugin index */
+if (session::global_is_set('plugin_index')) {
+  $old_plugin_index = session::global_get('plugin_index');
 } else {
-  $old_plugin_dir = "";
+  $old_plugin_index = '';
 }
 
 $plist->gen_menu();
@@ -140,19 +140,12 @@ if ($config->get_cfg_value("handleExpiredAccounts") == "TRUE") {
 }
 
 if (isset($_GET['plug']) && $plist->plugin_access_allowed($_GET['plug'])) {
-  $plug       = validate($_GET['plug']);
-  $plugin_dir = $plist->get_path($plug);
-  session::global_set('plugin_dir', $plugin_dir);
-  if ($plugin_dir == '') {
-    logging::log('security', 'fusiondirectory', '', array(), "main.php called with invalid plug parameter \"$plug\"");
-    header ('Location: index.php?signout=1&message=invalidparameter&plug='.$plug);
-    exit;
-  }
+  $plugin_index = validate($_GET['plug']);
 } else {
   /* set to welcome page as default plugin */
-  session::global_set('plugin_dir', 'welcome');
-  $plugin_dir = "$BASE_DIR/plugins/generic/welcome";
+  $plugin_index = 'welcome';
 }
+session::global_set('plugin_index', $plugin_index);
 
 /* Handle plugin locks.
     - Remove the plugin from session if we switched to another. (cleanup)
@@ -162,14 +155,9 @@ if (isset($_GET['plug']) && $plist->plugin_access_allowed($_GET['plug'])) {
 $cleanup      = FALSE;
 $remove_lock  = FALSE;
 
-/* Check if we have changed the selected plugin
-*/
-if ($old_plugin_dir != $plugin_dir && $old_plugin_dir != "") {
-  if (is_file("$old_plugin_dir/main.inc")) {
-    $cleanup = $remove_lock = TRUE;
-    require ("$old_plugin_dir/main.inc");
-    $cleanup = $remove_lock = FALSE;
-  }
+/* Check if we have changed the selected plugin */
+if (!empty($old_plugin_index) && ($old_plugin_index != $plugin_index)) {
+  pluglist::runMainInc($old_plugin_index, TRUE);
 } elseif ((isset($_GET['reset']) && $_GET['reset'] == 1) || isset($_POST['delete_lock'])) {
   /* Reset was posted, remove all created locks for the current plugin */
   $remove_lock = TRUE;
@@ -195,8 +183,8 @@ $lang = session::global_get('lang');
 $smarty->assign ('lang',  preg_replace('/_.*$/', '', $lang));
 $smarty->assign ('rtl',   Language::isRTL($lang));
 $smarty->assign ('must',  '<span class="must">*</span>');
-if (isset($plug)) {
-  $plug = "?plug=$plug";
+if (isset($plugin_index)) {
+  $plug = "?plug=$plugin_index";
 } else {
   $plug = "";
 }
@@ -234,28 +222,7 @@ if (($_SERVER['REQUEST_METHOD'] == 'POST')
 }
 
 /* Load plugin */
-if (is_file("$plugin_dir/main.inc")) {
-  $display = "";
-  try {
-    require ("$plugin_dir/main.inc");
-  } catch (Exception $e) {
-    $smarty->assign('header', print_header('geticon.php?context=status&icon=dialog-error&size=32', _('Fatal error!')));
-    $display = '<h1>'._('An unrecoverable error occurred. Please contact your administator.').'</h1><p>';
-    if (ini_get('display_errors') == 1) {
-      $display .= nl2br(htmlentities((string)$e, ENT_COMPAT, 'UTF-8'));
-    } else {
-      $display .= 'Error detail display is turned off.';
-    }
-    $display .= '</p>'."\n";
-  }
-} else {
-  msg_dialog::display(
-      _("Plugin"),
-      sprintf(_("Fatal error: Cannot find any plugin definitions for plugin '%s' ('%s' is not a file)!"), $plug, "$plugin_dir/main.inc"),
-      FATAL_ERROR_DIALOG);
-  exit();
-}
-
+pluglist::runMainInc($plugin_index);
 
 /* Print_out last ErrorMessage repeated string. */
 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
diff --git a/include/class_pluglist.inc b/include/class_pluglist.inc
index 3218dcbfb096b28d24caa060a9469298a7debae1..3986a8d4199358854d7dc342225d00fcbeb6f392 100644
--- a/include/class_pluglist.inc
+++ b/include/class_pluglist.inc
@@ -537,5 +537,48 @@ class pluglist {
 
     return $plist;
   }
+
+  static function runMainInc($index, $forceCleanup = FALSE)
+  {
+    global $BASE_DIR, $config, $plist, $ui, $smarty, $display, $remove_lock, $cleanup, $plug;
+
+    if ($index == 'welcome') {
+      $plugin_dir = "$BASE_DIR/plugins/generic/welcome";
+      $plugin     = $index;
+    } else {
+      $plugin_dir = $plist->get_path($index);
+      $plugin     = $plist->dirlist[$index];
+    }
+
+    try {
+      if ($forceCleanup) {
+        $cleanup = $remove_lock = TRUE;
+      }
+      if (is_file("$plugin_dir/main.inc")) {
+        $display = '';
+        require ("$plugin_dir/main.inc");
+      } elseif (is_callable(array($plugin, 'mainInc'))) {
+        $plugin::mainInc();
+      } else {
+        msg_dialog::display(
+          _('Plugin'),
+          sprintf(_("Fatal error: Cannot find any plugin definitions for plugin '%s' ('%s' is not a file)!"), $plugin, "$plugin_dir/main.inc"),
+          FATAL_ERROR_DIALOG
+        );
+        exit();
+      }
+      if ($forceCleanup) {
+        $cleanup = $remove_lock = FALSE;
+      }
+    } catch (Exception $e) {
+      $smarty->assign('header', print_header('geticon.php?context=status&icon=dialog-error&size=32', _('Fatal error!')));
+      $display = '<h1>'._('An unrecoverable error occurred. Please contact your administator.').'</h1><p>';
+      if (ini_get('display_errors') == 1) {
+        $display .= nl2br(htmlentities((string)$e, ENT_COMPAT, 'UTF-8'));
+      } else {
+        $display .= 'Error detail display is turned off.';
+      }
+      $display .= '</p>'."\n";
+    }
+  }
 }
-?>
diff --git a/include/management/class_management.inc b/include/management/class_management.inc
index a43c569949e7831ebd2c81af940978bddecf026b..7a09c9f62b9e3ff318271d6f996b980c4aba98eb 100644
--- a/include/management/class_management.inc
+++ b/include/management/class_management.inc
@@ -1217,10 +1217,14 @@ class management
   }
 
 
-  static function mainInc ($classname, $objectTypes = FALSE)
+  static function mainInc ($classname = NULL, $objectTypes = FALSE)
   {
     global $remove_lock, $cleanup, $display;
 
+    if ($classname === NULL) {
+      $classname = get_called_class();
+    }
+
     /* Remove locks */
     if ($remove_lock && session::is_set($classname)) {
       $macl = session::get($classname);