diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php
index 1f280623ce92eaa8d043592e484436446e270b0e..25976fa9e323e4756087773c936dc21c58205f2b 100644
--- a/apps/gallery/ajax/galleryOp.php
+++ b/apps/gallery/ajax/galleryOp.php
@@ -50,6 +50,7 @@ function handleGetThumbnails($albumname) {
 function handleGalleryScanning() {
   OC_DB::beginTransaction();
   set_time_limit(0);
+  OC_Gallery_Album::cleanup();
   $eventSource = new OC_EventSource();
   OC_Gallery_Scanner::scan($eventSource);
   $eventSource->close();
diff --git a/apps/gallery/lib/scanner.php b/apps/gallery/lib/scanner.php
index 1d74b1fb503646aaa7a5d8db7b81f6ff303b0c68..0fa4d08d3badba799ba33968d1b9957b7ed512b6 100644
--- a/apps/gallery/lib/scanner.php
+++ b/apps/gallery/lib/scanner.php
@@ -42,22 +42,22 @@ class OC_Gallery_Scanner {
 	// Scan single dir relative to gallery root
 	public static function scan($eventSource) {
 		$paths = self::findPaths();
-		$eventSource->send('count', count($paths));
+		$eventSource->send('count', count($paths)+1);
 		$owner = OC_User::getUser();
 		foreach ($paths as $path) {
 			$name = self::createName($path);
 			$images = self::findFiles($path);
 
-      $result = OC_Gallery_Album::find($owner, null, $path);
+			$result = OC_Gallery_Album::find($owner, null, $path);
 			// don't duplicate galleries with same path
 			if (!($albumId = $result->fetchRow())) {
-        OC_Gallery_Album::create($owner, $name, $path);
-        $result = OC_Gallery_Album::find($owner, $name);
+				OC_Gallery_Album::create($owner, $name, $path);
+				$result = OC_Gallery_Album::find($owner, $name);
 				$albumId = $result->fetchRow();
 			}
 			$albumId = $albumId['album_id'];
 			foreach ($images as $img) {
-        $result = OC_Gallery_Photo::find($albumId, $img);
+				$result = OC_Gallery_Photo::find($albumId, $img);
 				if (!$result->fetchRow())
 					OC_Gallery_Photo::create($albumId, $img);
 			}
@@ -65,6 +65,8 @@ class OC_Gallery_Scanner {
 				self::createThumbnails($name, $images);
 			$eventSource->send('scanned', '');
 		}
+		self::createIntermediateAlbums();
+		$eventSource->send('scanned', '');
 		$eventSource->send('done', 1);
 	}
 
@@ -81,17 +83,35 @@ class OC_Gallery_Scanner {
 		imagepng($thumbnail, OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/' . $albumName.'.png');
 	}
 
+	public static function createIntermediateAlbums() {
+		$paths = self::findPaths();
+		for ($i = 1; $i < count($paths); $i++) {
+			$prevLen = strlen($paths[$i-1]);
+			if (strncmp($paths[$i-1], $paths[$i], $prevLen)==0) {
+				$s = substr($paths[$i], $prevLen);
+				if (strrpos($s, '/') != 0) {
+					$a = explode('/', trim($s, '/'));
+					$p = $paths[$i-1];
+					foreach ($a as $e) {
+						$p .= ($p == '/'?'':'/').$e;
+						OC_Gallery_Album::create(OC_User::getUser(), $e, $p);
+					}
+				}
+			}
+		}
+	}
+
 	public static function isPhoto($filename) {
 		$ext = strtolower(substr($filename, strrpos($filename, '.')+1));
 		return $ext=='png' || $ext=='jpeg' || $ext=='jpg' || $ext=='gif';
 	}
 
 	public static function findFiles($path) {
-    $images = OC_FileCache::searchByMime('image','', OC_Filesystem::getRoot().$path);
+		$images = OC_FileCache::searchByMime('image','', OC_Filesystem::getRoot().$path);
 		$new = array();
 		foreach ($images as $i)
 			if (strpos($i, '/',1) === FALSE)
-        $new[] = $path.$i;
+				$new[] = $path.$i;
 		return $new;
 	}
 
@@ -100,8 +120,8 @@ class OC_Gallery_Scanner {
 		$paths=array();
 		foreach($images as $image){
 			$path=dirname($image);
-      $path = self::getGalleryRoot().($path=='.'?'':$path);
-      if ($path !== '/') $path=rtrim($path,'/');
+			$path = self::getGalleryRoot().($path=='.'?'':$path);
+			if ($path !== '/') $path=rtrim($path,'/');
 			if(array_search($path,$paths)===false){
 				$paths[]=$path;
 			}