diff --git a/template.php b/template.php
index 11cf503..5deccb0 100644
--- a/template.php
+++ b/template.php
@@ -662,12 +662,57 @@ if (!function_exists('drupal_html_id')) {
  * replace _zen_path() with drupal_get_path('theme', 'zen').
  */
 function _zen_path() {
-  static $path = FALSE;
-  if (!$path) {
-    $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
-    if (!empty($matches['zen']->filename)) {
-      $path = dirname($matches['zen']->filename);
+  return dirname(_zen_get_filename('theme', 'zen'));
+}
+
+/**
+ * Reimplementation of drupal_get_filename().
+ */
+function _zen_get_filename($type, $name, $filename = NULL) {
+  static $files = array();
+
+  if (!isset($files[$type])) {
+    $files[$type] = array();
+  }
+
+  if (!empty($filename) && file_exists($filename)) {
+    $files[$type][$name] = $filename;
+  }
+  elseif (isset($files[$type][$name])) {
+    // nothing
+  }
+  // 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))) {
+    $files[$type][$name] = $file;
+  }
+  else {
+    // Fallback to searching the filesystem if the database connection is
+    // not established or the requested file is not found.
+    $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;
+    }
+  }
+
+  if (isset($files[$type][$name])) {
+    return $files[$type][$name];
   }
-  return $path;
 }
