         "((localVisitDate > \(sixMonthsAgo)) OR (remoteVisitDate > \(sixMonthsAgo)))" +    // Exclude really old items.
         ") ORDER BY frecency DESC" +
         " LIMIT 1000"                                 // Don't even look at a huge set. This avoids work.
-
+        
         // Next: merge by domain and sum frecency, ordering by that sum and reducing to a (typically much lower) limit.
         let historySQL =
         "SELECT historyID, url, title, guid, domain_id, domain" +

                                                     groupClause: String = "GROUP BY historyID ",
                                                     whereData: String? = nil,
                                                     includeIcon: Bool = true) -> Deferred<Maybe<Cursor<Site>>> {
        let localFrecencySQL = getLocalFrecencySQL()
        let remoteFrecencySQL = getRemoteFrecencySQL()
        let sixMonthsInMicroseconds: UInt64 = 15_724_800_000_000      // 182 * 1000 * 1000 * 60 * 60 * 24


    public func getSitesByFrecencyWithLimit(limit: Int, includeIcon: Bool) -> Deferred<Maybe<Cursor<Site>>> {
        // Exclude redirect domains. Bug 1194852.
        let (whereData, groupBy) = self.topSiteClauses()
        return self.getFilteredSitesByFrecencyWithLimit(limit, groupClause: groupBy, whereData: whereData, includeIcon: includeIcon)
    }

    public func getTopSitesWithLimit(limit: Int) -> Deferred<Maybe<Cursor<Site>>> {
        let purgeTopSites: () -> Success = {
            let deleteQuery = "DELETE FROM \(TableCachedTopSites)"
            return self.db.run(deleteQuery, withArgs: nil)
        }

        let insertTopSites: () -> Success = { frecencyResults in
            let (whereData, groupBy) = self.topSiteClauses()
            let (query, args) = self.filteredSitesByFrecencyQueryWithLimit(limit, groupClause: groupBy, whereData: whereData)
            let insertQuery = "INSERT INTO \(TableCachedTopSites) \(query)"
            return self.db.run(insertQuery, withArgs: args)
        }

        let topSitesResult: () -> Deferred<Maybe<Cursor<Site>>> = {
            // Grab all of the entries for top sites
            let topSitesQuery = "SELECT * FROM \(TableCachedTopSites) ORDER BY frecencies DESC LIMIT (?)"
            let factory = SQLiteHistory.iconHistoryColumnFactory
            return self.db.runQuery(topSitesQuery, args: [limit], factory: factory)
        }

        let lastComputeTime: Timestamp = 0

        // If the cached data is too stale, dump everything, recompute, cache, and return
        let now = NSDate.now()
        if now - lastComputeTime > OneHourInMilliseconds {
            return (purgeTopSites() >>> insertTopSites) >>> topSitesResult
        } else {
            return topSitesResult()
        }
    }

    public func getSitesByFrecencyWithLimit(limit: Int, whereURLContains filter: String) -> Deferred<Maybe<Cursor<Site>>> {
