diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 3c8d121..51ab393 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -172,6 +172,8 @@ function install_state_defaults() {
     'config_verified' => FALSE,
     // TRUE when there is a valid database connection.
     'database_verified' => FALSE,
+    // TRUE if database is empty & ready to install
+    'database_ready' => FALSE,
     // TRUE when a valid settings.php exists (containing both database
     // connection information and config directory names).
     'settings_verified' => FALSE,
@@ -281,7 +283,7 @@ function install_begin_request(&$install_state) {
   // The user agent header is used to pass a database prefix in the request when
   // running tests. However, for security reasons, it is imperative that no
   // installation be permitted using such a prefix.
-  if ($install_state['interactive'] && strpos($request->server->get('HTTP_USER_AGENT'), 'simpletest') !== FALSE && !drupal_valid_test_ua()) {
+    if ($install_state['interactive'] && strpos($request->server->get('HTTP_USER_AGENT'), 'simpletest') !== FALSE && !drupal_valid_test_ua()) {
     header($request->server->get('SERVER_PROTOCOL') . ' 403 Forbidden');
     exit;
   }
@@ -321,9 +323,7 @@ function install_begin_request(&$install_state) {
   $container
     ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
     ->addArgument(new Reference('language_manager'));
-  $container
-    ->register('path.matcher', 'Drupal\Core\Path\PathMatcher')
-    ->addArgument(new Reference('config.factory'));
+
 
   \Drupal::setContainer($container);
 
@@ -332,6 +332,15 @@ function install_begin_request(&$install_state) {
   $install_state['database_verified'] = install_verify_database_settings();
   $install_state['settings_verified'] = $install_state['config_verified'] && $install_state['database_verified'];
 
+  // installing factory tables only after the database check
+  if ($install_state['database_verified'] && $install_state['database_ready']) {
+    $container
+      ->register('path.matcher', 'Drupal\Core\Path\PathMatcher')
+      ->addArgument(new Reference('config.factory'));
+  }
+
+  $install_state['settings_verified'] = $install_state['config_verified'] && $install_state['database_verified'];
+
   if ($install_state['settings_verified']) {
     try {
       $system_schema = system_schema();
@@ -365,6 +374,10 @@ function install_begin_request(&$install_state) {
   $container = $kernel->getContainer();
   $container->get('request_stack')->push($request);
 
+
+
+
+
   // Register the file translation service.
   if (isset($GLOBALS['config']['locale.settings']['translation.path'])) {
     $directory = $GLOBALS['config']['locale.settings']['translation.path'];
@@ -679,6 +692,11 @@ function install_tasks($install_state) {
       'run' => $install_state['settings_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
       'function' => 'Drupal\Core\Installer\Form\SiteSettingsForm',
     ),
+    'install_database_ready' => array(
+      'run' => $install_state['database_ready'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
+      //'function' => install_database_ready(),
+
+    ),
     'install_base_system' => array(
       'run' => $install_state['base_system_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
     ),
@@ -967,6 +985,7 @@ function install_base_system(&$install_state) {
   // Enable the user module so that sessions can be recorded during the
   // upcoming bootstrap step.
   \Drupal::moduleHandler()->install(array('user'), FALSE);
+  \Drupal::service('router.builder')->rebuild();
 
   // Save the list of other modules to install for the upcoming tasks.
   // State can be set to the database now that system.module is installed.
@@ -1007,6 +1026,7 @@ function install_verify_completed_task() {
  * Verifies that settings.php specifies a valid database connection.
  */
 function install_verify_database_settings() {
+
   if ($database = Database::getConnectionInfo()) {
     $database = $database['default'];
     $settings_file = './' . conf_path(FALSE) . '/settings.php';
@@ -1019,6 +1039,22 @@ function install_verify_database_settings() {
 }
 
 /**
+ * Check if database is ready (assuming no drupal previously installed)
+ * @param $install_state
+ */
+function install_database_ready($install_state) {
+  $system_schema = system_schema();
+  end($system_schema);
+  $table = key($system_schema);
+
+  if ($database = Database::getConnectionInfo()) {
+    if (Database::getConnection()->schema()->tableExists($table)) {
+      throw new AlreadyInstalledException(\Drupal::service('string_translation'));
+    }
+  }
+}
+
+/**
  * Checks a database connection and returns any errors.
  */
 function install_database_errors($database, $settings_file) {
