--- includes/path.inc	2010-05-28 08:12:04.000000000 -0700
+++ includes/path.inc.patched	2010-11-14 14:38:56.000000000 -0800
@@ -66,9 +66,16 @@ function drupal_lookup_path($action, $pa
         return $map[$path_language][$path];
       }
       // Get the most fitting result falling back with alias without language
-      $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $path, $path_language));
-      $map[$path_language][$path] = $alias;
-      return $alias;
+      $dst = '';
+      $result = db_query("SELECT dst, language FROM {url_alias} WHERE src = '%s'", $path);
+      while ($alias = db_fetch_object($result)) {
+        // I'm pretty sure the logic below is not correct, see correct analog on line 95
+        if ((isset($alias->language) && $alias->language == $path_language)
+	 || (empty($map[$path_language][$path]) && strpos($path, 'node/') === 0) && $alias->language == '') {
+          $dst = $map[$path_language][$path] = $alias->dst;
+        }
+      }
+      return $dst;
     }
     // Check $no_src for this $path in case we've already determined that there
     // isn't a path that has this alias
@@ -77,10 +84,13 @@ function drupal_lookup_path($action, $pa
       $src = '';
       if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
         // Get the most fitting result falling back with alias without language
-        if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $path, $path_language))) {
-          $map[$path_language][$src] = $path;
+        $result = db_query("SELECT src, language FROM {url_alias} WHERE dst = '%s'", $path);
+        while ($alias = db_fetch_object($result)) {
+          if ((isset($alias->language) && $alias->language == $path_language) || (empty($map[$path_language][$path]) && $alias->language == '')) {
+            $src = $map[$path_language][$path] = $alias->src;
+          }
         }
-        else {
+	if (empty($src)) {
           // We can't record anything into $map because we do not have a valid
           // index and there is no need because we have not learned anything
           // about any Drupal path. Thus cache to $no_src.
