diff --git a/includes/install.inc b/includes/install.inc
index c4bcb88..51d0cdd 100644
--- a/includes/install.inc
+++ b/includes/install.inc
@@ -1272,6 +1272,7 @@ function drupal_check_module($module) {
  *   The info array.
  */
 function install_profile_info($profile, $locale = 'en') {
+  include_once DRUPAL_ROOT . '/includes/module.inc';
   $cache = &drupal_static(__FUNCTION__, array());
 
   if (!isset($cache[$profile])) {
@@ -1285,15 +1286,15 @@ function install_profile_info($profile, $locale = 'en') {
       'php' => DRUPAL_MINIMUM_PHP,
     );
     $info = drupal_parse_info_file("profiles/$profile/$profile.info") + $defaults;
-    $info['dependencies'] = array_unique(array_merge(
-      drupal_required_modules(),
-      $info['dependencies'],
-      ($locale != 'en' && !empty($locale) ? array('locale') : array()))
-    );
-
+    if ($locale != 'en' && !empty($locale) && !in_array('locale', $info['dependencies'])) {
+      $info['dependencies'][] = 'locale';
+    }
+    $info['dependencies'] = module_build_dependencies($info['dependencies']);
+    $required_modules = drupal_required_modules();
     // drupal_required_modules() includes the current profile as a dependency.
     // Since a module can't depend on itself we remove that element of the array.
-    array_shift($info['dependencies']);
+    array_shift($required_modules);
+    $info['dependencies'] = array_unique(array_merge($required_modules, $info['dependencies']));
 
     $cache[$profile] = $info;
   }
@@ -1301,6 +1302,54 @@ function install_profile_info($profile, $locale = 'en') {
 }
 
 /**
+ * Given a list of modules, resorts and adds depedencies.
+ *
+ * @param $module_list
+ *   An array of module names.
+ * @param $include_enabled
+ *   Include enabled modules.
+ *
+ * @return
+ *   An array of modules with their depedencies, in install order.
+ *
+ * @see module_enable()
+ */
+function module_build_dependencies($module_list, $include_enabled = TRUE) {
+  if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install') {
+    $module_data = _module_build_dependencies(_system_rebuild_module_data());
+  }
+  else {
+    $module_data = system_rebuild_module_data();
+  }
+
+  // Create an associative array with weights as values.
+  $module_list = array_flip(array_values($module_list));
+
+  while (list($module) = each($module_list)) {
+    if (!isset($module_data[$module])) {
+      // This module is not found in the filesystem.
+      continue;
+    }
+    if (!$include_enabled && $module_data[$module]->status) {
+      // Skip already enabled modules.
+      unset($module_list[$module]);
+      continue;
+    }
+    $module_list[$module] = $module_data[$module]->sort;
+
+    // Add dependencies to the list, with a placeholder weight.
+    // The new modules will be processed as the while loop continues.
+    foreach (array_keys($module_data[$module]->requires) as $dependency) {
+      if (!isset($module_list[$dependency])) {
+        $module_list[$dependency] = 0;
+      }
+    }
+  }
+  arsort($module_list);
+  return array_keys($module_list);
+}
+
+/**
  * Ensures the environment for a Drupal database on a predefined connection.
  *
  * This will run tasks that check that Drupal can perform all of the functions
