Index: wp-includes/theme.php
===================================================================
--- wp-includes/theme.php	(revision 19969)
+++ wp-includes/theme.php	(working copy)
@@ -578,8 +578,7 @@
 	if ( empty( $wp_theme_directories ) )
 		return false;
 
-	$theme_files = array();
-	$wp_broken_themes = array();
+	$found_themes = $wp_broken_themes = array();
 
 	/* Loop the registered theme directories and extract all themes */
 	foreach ( (array) $wp_theme_directories as $theme_root ) {
@@ -590,62 +589,56 @@
 			$theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
 
 		/* Files in the root of the current theme directory and one subdir down */
-		$themes_dir = @ opendir($theme_root);
+		$themes_directory = @ scandir( $theme_root );
 
-		if ( !$themes_dir )
+		if ( ! $themes_directory )
 			return false;
 
-		while ( ($theme_dir = readdir($themes_dir)) !== false ) {
-			if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
-				if ( $theme_dir[0] == '.' || $theme_dir == 'CVS' )
-					continue;
+		$found_theme = false;
+		foreach ( $themes_directory as $theme_dir ) {
+			if ( is_dir( $theme_root . '/' . $theme_dir ) && ( $theme_dir[0] == '.' || $theme_dir == 'CVS' ) )
+				continue;
+			if ( ! file_exists( $theme_root . '/' . $theme_dir . '/style.css' ) )
+				continue;
+			$found_themes[ $theme_dir ] = array(
+				'theme_file' => $theme_dir . '/style.css',
+				'theme_root' => $theme_root,
+			);
+			$found_theme = true;
+		}
 
-				$stylish_dir = @opendir($theme_root . '/' . $theme_dir);
-				$found_stylesheet = false;
+		if ( ! $found_theme ) {
+			$sub_theme_dir = $theme_dir;
+			$sub_theme_root = $theme_root . '/' . $sub_theme_dir;
+			$themes_directory = @ scandir( $sub_theme_root );
 
-				while ( ($theme_file = readdir($stylish_dir)) !== false ) {
-					if ( $theme_file == 'style.css' ) {
-						$theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
-						$found_stylesheet = true;
-						break;
-					}
-				}
-				@closedir($stylish_dir);
+			if ( ! $themes_directory )
+				return false;
 
-				if ( !$found_stylesheet ) { // look for themes in that dir
-					$subdir = "$theme_root/$theme_dir";
-					$subdir_name = $theme_dir;
-					$theme_subdirs = @opendir( $subdir );
+			$found_theme = false;
+			foreach ( $themes_directory as $theme_dir ) {
+				if ( is_dir( $sub_theme_root . '/' . $theme_dir ) && ( $theme_dir[0] == '.' || $theme_dir == 'CVS' ) )
+					continue;
+				if ( ! file_exists( $sub_theme_root . '/' . $theme_dir . '/style.css' ) )
+					continue;
+				$found_themes[ $sub_theme_dir . '/' . $theme_dir ] = array(
+					'theme_file' => $sub_theme_dir . '/' . $theme_dir . '/style.css',
+					'theme_root' => $theme_root,
+				);
+				$found_theme = true;
+			}
 
-					$found_subdir_themes = false;
-					while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
-						if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
-							if ( $theme_subdir[0] == '.' || $theme_subdir == 'CVS' )
-								continue;
-
-							$stylish_dir = @opendir($subdir . '/' . $theme_subdir);
-							$found_stylesheet = false;
-
-							while ( ($theme_file = readdir($stylish_dir)) !== false ) {
-								if ( $theme_file == 'style.css' ) {
-									$theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
-									$found_stylesheet = true;
-									$found_subdir_themes = true;
-									break;
-								}
-							}
-							@closedir($stylish_dir);
-						}
-					}
-					@closedir($theme_subdirs);
-					if ( !$found_subdir_themes )
-						$wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
-				}
+			if ( ! $found_theme ) {
+				$wp_broken_themes[ $sub_theme_dir ] = array(
+					'Name' => $sub_theme_dir,
+					'Title' => $theme_dir,
+					'Description' => __( 'Stylesheet is missing.' ),
+				);
 			}
 		}
-		@closedir( $themes_dir );
 	}
-	return $theme_files;
+
+	return $found_themes;
 }
 
 /**
