diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 01ae311..a3a41ec 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -514,22 +514,33 @@ function drupal_get_filename($type, $name, $filename = NULL) {
   // Verify that we have an active database connection, before querying
   // the database.  This is required because this function is called both
   // before we have a database connection (i.e. during installation) and
-  // when a database connection fails.
-  elseif (db_is_active() && (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file))) {
+  // when a database connection fails. During install, there is a database
+  // connection but not necessarily a system table, so call db_query with @
+  // to allow installs to continue when this function is called. 
+  elseif (db_is_active() && (($file = db_result(@db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file))) {
     $files[$type][$name] = $file;
   }
   else {
     // Fallback to searching the filesystem if the database connection is
     // not established or the requested file is not found.
-    $config = conf_path();
-    $dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s");
-    $file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type");
-
-    foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) {
-      if (file_exists($file)) {
-        $files[$type][$name] = $file;
-        break;
-      }
+    $dir = $type .'s';
+    if ($type == 'theme_engine') {
+      $dir = 'themes/engines';
+      $mask = $name .'\.engine$';
+    }
+    elseif ($type == 'theme') {
+      $mask = $name .'\.info$';
+    }
+    else {
+      $mask = $name .'\.'. $type .'$';
+    }
+
+    if (!function_exists('drupal_system_listing')) {
+      require_once './includes/common.inc';
+    }
+    $matches = drupal_system_listing($mask, $dir, 'name', 0);
+    if (!empty($matches[$name]->filename)) {
+      $files[$type][$name] = $matches[$name]->filename;
     }
   }
 
diff --git a/includes/common.inc b/includes/common.inc
index 73d97f1..1a37d07 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2827,6 +2827,9 @@ function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1)
   }
 
   // Get current list of items
+  if (!function_exists('file_scan_directory')) {
+    require_once './includes/file.inc';
+  }
   foreach ($searchdir as $dir) {
     $files = array_merge($files, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, $key, $min_depth));
   }
