     }
     
     func stopPeriodicUpdates(sdkKey: String) {
-        if let timer = timers[sdkKey] {
-            logger?.log(level: .info, message: "Stopping timer for datafile updates sdkKey:" + sdkKey)
-            
-            timer.invalidate()
-            timers.removeValue(forKey: sdkKey)
-        }
-
+        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() {
-        for key in timers.keys {
-            logger?.log(level: .info, message: "Stopping timer for all datafile updates")
-            stopPeriodicUpdates(sdkKey: key)
+        timers.performAtomic { (timers) in
+            for key in timers.keys {
+                logger?.log(level: .info, message: "Stopping timer for all datafile updates")
+                stopPeriodicUpdates(sdkKey: key)
+            }
         }
         
     }

class DefaultDatafileHandler : OPTDatafileHandler {
    static public var endPointStringFormat = "https://cdn.optimizely.com/datafiles/%@.json"
    lazy var logger = HandlerRegistryService.shared.injectLogger()
    var timers:[String:Timer] = [String:Timer]()
    let dataStore = DataStoreUserDefaults()
    
    required init() {

    
    func downloadDatafile(sdkKey: String) -> Data? {
        
        var datafile:Data?
        let group = DispatchGroup()
        
        group.enter()
        
        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)
