             }
         }
     }
-    private func fetchFromServer(complete:()->()) {
+    typealias FetchComplete = ()->()
+    private func fetchFromServer(complete:FetchComplete) {
         let headers = ["Accept":"application/json"]
         Alamofire.request(.GET, "http://www.lgtm.in/g", headers:headers).validate().responseJSON {req, res, result in
             switch result {
             case .Success:
             if let json = result.value, url = json["actualImageUrl"] as? String {
-                Alamofire.request(.GET, url).validate(contentType: ["image/*"])
-                    .responseData
-                { req, res, result in
-                    switch result {
-                    case .Success(let value):
-                    if let image = NSImage(data: value) {
-                        let lgtm = Lgtm(url: url, image:image)
-                        self.stack.append(lgtm)
-                        complete()
-                    }
-                    case .Failure(_, let error):
-                        print(error)
-                    }
+                self.fetchImage(url) { image in
+                    let lgtm = Lgtm(url: url, image:image)
+                    self.stack.append(lgtm)
+                    complete()
                 }
             }
             case .Failure(_, let error):
                 print(error)
                 break
+            }
+        }
+    }
+    typealias FetchImageComplete = NSImage->()
+    private func fetchImage(url:String, complete:FetchImageComplete) {
+        Alamofire.request(.GET, url).validate(contentType: ["image/*"])
+            .responseData
+        { req, res, result in
+            switch result {
+            case .Success(let value):
+            if let image = NSImage(data: value) {
+                complete(image)
+            }
+            case .Failure(_, let error):
+                print(error)
             }
         }
     }

        guard !sharedInstance.fetching else { return }
        sharedInstance.fetching = true
        sharedInstance.fetchLgtmFromServer()
    }
}
/// private interfaces

import RealmSwift
/// interact with Realm
extension Provider {
    func getFavoriteLgtms() -> [Lgtm] {
        return favStack
    }
    private func fetchFromRealm() {
        let realm = getRealm()
        let urls = realm.objects(RealmLgtm).map{$0 as RealmLgtm}.map {$0.url}
        let targetUrls:[String]
        if urls.count > stackLimit {
            targetUrls = [] + urls[0...(stackLimit - 1)]
        } else {
            targetUrls = urls
        }
        for url in targetUrls {
            fetchImage(url) { [unowned self] image in
                let lgtm = Lgtm(url: url, image:image)
                self.favStack.append(lgtm)
            }
        }
    }
    private func saveToRealm(lgtm:Lgtm) {
        let data = RealmLgtm(lgtm: lgtm)
        let realm = getRealm()
        realm.write {
            realm.add(data, update: true)
        }
    }
    private func getRealm() -> Realm {
        let realm:Realm
        do {
            realm = try Realm()
        } catch {
            print(error)
            print(Realm.defaultPath)
            fatalError()
        }
        return realm
    }
}
class RealmLgtm : Object {
