diff --git a/api/index.php b/api/index.php
index 8d69e1a7351bd63383b3372fceafa9384fda1e52..ae4f1eb5df6c566efb94f4e1afe10fd712b4c904 100644
--- a/api/index.php
+++ b/api/index.php
@@ -100,95 +100,7 @@
 			$limit = (int) db_escape_string($_REQUEST["limit"]);
 			$offset = (int) db_escape_string($_REQUEST["offset"]);
 
-			if ($limit) {
-				$limit_qpart = "LIMIT $limit OFFSET $offset";
-			} else {
-				$limit_qpart = "";
-			}
-
-			if (!$cat_id) {
-				$result = db_query($link, "SELECT 
-					id, feed_url, cat_id, title, ".
-						SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
-						FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] . 
-						" ORDER BY cat_id, title " . $limit_qpart);
-			} else {
-				$result = db_query($link, "SELECT 
-					id, feed_url, cat_id, title, ".
-						SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
-						FROM ttrss_feeds WHERE 
-						cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"] . 
-						" ORDER BY cat_id, title " . $limit_qpart);
-			}
-
-			$feeds = array();
-
-			while ($line = db_fetch_assoc($result)) {
-
-				$unread = getFeedUnread($link, $line["id"]);
-
-				$icon_path = "../" . ICONS_DIR . "/" . $line["id"] . ".ico";
-				$has_icon = file_exists($icon_path) && filesize($icon_path) > 0;
-
-				if ($unread || !$unread_only) {
-
-					$row = array(
-							"feed_url" => $line["feed_url"],
-							"title" => $line["title"],
-							"id" => (int)$line["id"],
-							"unread" => (int)$unread,
-							"has_icon" => $has_icon,
-							"cat_id" => (int)$line["cat_id"],
-							"last_updated" => strtotime($line["last_updated"])
-						);
-	
-					array_push($feeds, $row);
-				}
-			}
-
-			/* Labels */
-
-			if (!$cat_id || $cat_id == -2) {
-				$counters = getLabelCounters($link, true);
-
-				foreach (array_keys($counters) as $id) {
-
-					$unread = $counters[$id]["counter"];
-	
-					if ($unread || !$unread_only) {
-	
-						$row = array(
-								"id" => $id,
-								"title" => $counters[$id]["description"],
-								"unread" => $counters[$id]["counter"],
-								"cat_id" => -2,
-							);
-	
-						array_push($feeds, $row);
-					}
-				}
-			}
-
-			/* Virtual feeds */
-
-			if (!$cat_id || $cat_id == -1) {
-				foreach (array(-1, -2, -3, -4, 0) as $i) {
-					$unread = getFeedUnread($link, $i);
-
-					if ($unread || !$unread_only) {
-						$title = getFeedTitle($link, $i);
-
-						$row = array(
-								"id" => $i,
-								"title" => $title,
-								"unread" => $unread,
-								"cat_id" => -1,
-							);
-						array_push($feeds, $row);
-					}
-
-				}
-			}
+			$feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
 
 			print json_encode($feeds);
 
@@ -226,47 +138,8 @@
 			/* all_articles, unread, adaptive, marked, updated */
 			$view_mode = db_escape_string($_REQUEST["view_mode"]);
 
-			/* do not rely on params below */
-
-			$search = db_escape_string($_REQUEST["search"]);
-			$search_mode = db_escape_string($_REQUEST["search_mode"]);
-			$match_on = db_escape_string($_REQUEST["match_on"]);
-			
-			$qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, 
-				$view_mode, $is_cat, $search, $search_mode, $match_on,
-				false, $offset);
-
-			$result = $qfh_ret[0];
-			$feed_title = $qfh_ret[1];
-
-			$headlines = array();
-
-			while ($line = db_fetch_assoc($result)) {
-				$is_updated = ($line["last_read"] == "" && 
-					($line["unread"] != "t" && $line["unread"] != "1"));
-
-				$headline_row = array(
-						"id" => (int)$line["id"],
-						"unread" => sql_bool_to_bool($line["unread"]),
-						"marked" => sql_bool_to_bool($line["marked"]),
-						"updated" => strtotime($line["updated"]),
-						"is_updated" => $is_updated,
-						"title" => $line["title"],
-						"link" => $line["link"],
-						"feed_id" => $line["feed_id"],
-					);
-
-				if ($show_excerpt) {
-					$excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
-					$headline_row["excerpt"] = $excerpt;
-				}
-
-				if ($show_content) {
-					$headline_row["content"] = $line["content_preview"];
-				}
-
-				array_push($headlines, $headline_row);
-			}
+			$headlines = api_get_headlines($link, $feed_id, $limit, $offset,
+				$filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
 
 			print json_encode($headlines);
 
diff --git a/functions.php b/functions.php
index 05eb85909c9e6c54b3b8fbe64448e5a78419c5ca..78e0d4f1800fcd35d5a5c74fd32b97592ee7f83a 100644
--- a/functions.php
+++ b/functions.php
@@ -6652,4 +6652,145 @@
 		return $rv;
 	}
 
+	function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset) {
+			if ($limit) {
+				$limit_qpart = "LIMIT $limit OFFSET $offset";
+			} else {
+				$limit_qpart = "";
+			}
+
+			if (!$cat_id) {
+				$result = db_query($link, "SELECT 
+					id, feed_url, cat_id, title, ".
+						SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
+						FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] . 
+						" ORDER BY cat_id, title " . $limit_qpart);
+			} else {
+				$result = db_query($link, "SELECT 
+					id, feed_url, cat_id, title, ".
+						SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
+						FROM ttrss_feeds WHERE 
+						cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"] . 
+						" ORDER BY cat_id, title " . $limit_qpart);
+			}
+
+			$feeds = array();
+
+			while ($line = db_fetch_assoc($result)) {
+
+				$unread = getFeedUnread($link, $line["id"]);
+
+				$has_icon = feed_has_icon($line['id']);
+
+				if ($unread || !$unread_only) {
+
+					$row = array(
+							"feed_url" => $line["feed_url"],
+							"title" => $line["title"],
+							"id" => (int)$line["id"],
+							"unread" => (int)$unread,
+							"has_icon" => $has_icon,
+							"cat_id" => (int)$line["cat_id"],
+							"last_updated" => strtotime($line["last_updated"])
+						);
+	
+					array_push($feeds, $row);
+				}
+			}
+
+			/* Labels */
+
+			if (!$cat_id || $cat_id == -2) {
+				$counters = getLabelCounters($link, true);
+
+				foreach (array_keys($counters) as $id) {
+
+					$unread = $counters[$id]["counter"];
+	
+					if ($unread || !$unread_only) {
+	
+						$row = array(
+								"id" => $id,
+								"title" => $counters[$id]["description"],
+								"unread" => $counters[$id]["counter"],
+								"cat_id" => -2,
+							);
+	
+						array_push($feeds, $row);
+					}
+				}
+			}
+
+			/* Virtual feeds */
+
+			if (!$cat_id || $cat_id == -1) {
+				foreach (array(-1, -2, -3, -4, 0) as $i) {
+					$unread = getFeedUnread($link, $i);
+
+					if ($unread || !$unread_only) {
+						$title = getFeedTitle($link, $i);
+
+						$row = array(
+								"id" => $i,
+								"title" => $title,
+								"unread" => $unread,
+								"cat_id" => -1,
+							);
+						array_push($feeds, $row);
+					}
+
+				}			
+			}
+		return $feeds;
+	}
+
+	function api_get_headlines($link, $feed_id, $limit, $offset,
+					$filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order) {
+
+			/* do not rely on params below */
+
+			$search = db_escape_string($_REQUEST["search"]);
+			$search_mode = db_escape_string($_REQUEST["search_mode"]);
+			$match_on = db_escape_string($_REQUEST["match_on"]);
+			
+			$qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, 
+				$view_mode, $is_cat, $search, $search_mode, $match_on,
+				$order, $offset);
+
+			$result = $qfh_ret[0];
+			$feed_title = $qfh_ret[1];
+
+			$headlines = array();
+
+			while ($line = db_fetch_assoc($result)) {
+				$is_updated = ($line["last_read"] == "" && 
+					($line["unread"] != "t" && $line["unread"] != "1"));
+
+				$headline_row = array(
+						"id" => (int)$line["id"],
+						"unread" => sql_bool_to_bool($line["unread"]),
+						"marked" => sql_bool_to_bool($line["marked"]),
+						"updated" => strtotime($line["updated"]),
+						"is_updated" => $is_updated,
+						"title" => $line["title"],
+						"link" => $line["link"],
+						"feed_id" => $line["feed_id"],
+						"has_icon" => feed_has_icon($line["feed_id"])
+					);
+
+				if ($show_excerpt) {
+					$excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
+					$headline_row["excerpt"] = $excerpt;
+				}
+
+				if ($show_content) {
+					$headline_row["content"] = $line["content_preview"];
+				}
+
+				array_push($headlines, $headline_row);
+			}
+
+			return $headlines;
+	}
+
 ?>
diff --git a/modules/backend-rpc.php b/modules/backend-rpc.php
index b21e161e41d2cb99ad18cfd94494a3bfdff5d8a2..f8233a7cd51495abe65a356b7dbed35b330ad4ea 100644
--- a/modules/backend-rpc.php
+++ b/modules/backend-rpc.php
@@ -978,6 +978,42 @@
 			return;
 		}
 
+		if ($subop == "digest-init") {
+			print "<rpc-reply>";
+
+			$tmp_feeds = api_get_feeds($link, false, true, false, 0);
+			$feeds = array();
+
+			foreach ($tmp_feeds as $f) {
+				if ($f['id'] > 0) array_push($feeds, $f);
+			}
+
+			function feeds_sort_by_unread_rev($a, $b) {
+				$a = $a['unread'];
+				$b = $b['unread'];
+
+				if ($a == $b) {
+					return 0;
+				}
+				return ($a < $b) ? 1 : -1;
+			}
+
+//			uasort($feeds, 'feeds_sort_by_unread_rev');
+//			$feeds = array_slice($feeds, 0, 10);
+
+			print "<feeds><![CDATA[" . json_encode($feeds) . "]]></feeds>";
+
+			$headlines = api_get_headlines($link, -4, 20, 0,
+				'', true, true, false, "all_articles", "updated DESC");
+
+			//function api_get_headlines($link, $feed_id, $limit, $offset,
+			//		$filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
+
+			print "<headlines><![CDATA[" . json_encode($headlines) . "]]></headlines>";
+			print "</rpc-reply>";
+			return;
+		}
+
 		print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
 	}
 ?>