     
     func downloadDatafile(sdkKey: String) -> Data? {
         
-        let config = URLSessionConfiguration.ephemeral
-        let session = URLSession(configuration: config)
-        let str = String(format: DefaultDatafileHandler.endPointStringFormat, sdkKey)
-        var result:Data?
+        var datafile:Data?
         let group = DispatchGroup()
         
         group.enter()
         
-        if let url = URL(string: str) {
-            let task = session.downloadTask(with: url){ (url, response, error) in
-                self.logger?.log(level: .debug, message: response.debugDescription)
-                if let response = response as? HTTPURLResponse, response.statusCode == 200,
-                    let url = url, let projectConfig = try? Data(contentsOf: url) {
-                    result = projectConfig
-                    self.saveDatafile(sdkKey: sdkKey, dataFile: projectConfig)
-                }
-                group.leave()
-            }
-            
-            task.resume()
-            
-            group.wait()
-            
-        }
-        return result
-    }
-    
-    open func downloadDatafile(sdkKey: String, completionHandler: @escaping (Result<Data?, DatafileDownloadError>) -> Void) {
+        downloadDatafile(sdkKey: sdkKey) { (result) in
+            switch result {
+            case .success(let data):
+                datafile = data
+            case .failure(let error):
+                self.logger?.log(level: .error, message: error.localizedDescription)
+            }
+            group.leave()
+        }
+        
+        group.wait()
+        
+        return datafile
+    }
+    
+    open func downloadDatafile(sdkKey: String, completionHandler: @escaping DatafileDownloadCompletionHandler) {
         let config = URLSessionConfiguration.ephemeral
         let session = URLSession(configuration: config)
         let str = String(format: DefaultDatafileHandler.endPointStringFormat, sdkKey)

    }
    
    func startPeriodicUpdates(sdkKey: String, updateInterval: Int, datafileChangeNotification:((Data)->Void)?) {
        if let _ = timers[sdkKey] {
            logger?.log(level: .info, message: "Timer already started for datafile updates")
            return
        }
        if #available(iOS 10.0, tvOS 10.0, *) {
            DispatchQueue.main.async {
                let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(updateInterval), repeats: true) { (timer) in
                    self.downloadDatafile(sdkKey: sdkKey) { (result) in
                        if let datafileChangeNotification = datafileChangeNotification {
                            switch result {
                            case .success(let data):
                                if let data = data {
                                    datafileChangeNotification(data)
                                }
                            case .failure( _): break
                                // don't do anything.
                            }
                        }
                        // background download saves to cache
                    }
                }
                self.timers[sdkKey] = timer

            }
        } else {
            // Fallback on earlier versions

    }
    
    func stopPeriodicUpdates(sdkKey: String) {
        timers.performAtomic { (timers) in
            if let timer = timers[sdkKey] {
                logger?.log(level: .info, message: "Stopping timer for datafile updates sdkKey: \(sdkKey)")
                
                timer.invalidate()
                timers.removeValue(forKey: sdkKey)
            }

        }
    }
    
    func stopPeriodicUpdates() {
        timers.performAtomic { (timers) in
            for key in timers.keys {
                logger?.log(level: .info, message: "Stopping timer for all datafile updates")
                stopPeriodicUpdates(sdkKey: key)
            }
        }
        
    }
