     }
     
     public lazy var mainContext: NSManagedObjectContext = {
-        let context = self.stack.mainContext!
+        let context = self.stack.mainContext
         context.name = "Main"
         return context
     }()

        }
        return dbFolder
    }()
}

private let SavingContextName = "Saving"
private let MainContextName = "Main"
private class LegacyCoreStack: CoreStack {
    struct StackConfig {
        var storeType: String!
        var storeURL: URL!
        var options: [NSObject: AnyObject]?
    }

    fileprivate var mainContext: NSManagedObjectContext! {
        return managedObjectContext
    }
    
    private let modelName: String
    private let storeType: String
    private let directory: FileManager.SearchPathDirectory
    private var writingContext: NSManagedObjectContext?
    private var wipeDatabaseOnConflict = false
    private let mergePolicy: NSMergePolicyType
    fileprivate let identifier: String
    
    private static var spawnedBackgroundCount = 0
    
    init(modelName: String, type: String = NSSQLiteStoreType, identifier: String, in directory: FileManager.SearchPathDirectory = .documentDirectory, mergePolicy: NSMergePolicyType = .mergeByPropertyObjectTrumpMergePolicyType, wipeOnConflict: Bool) {
        self.modelName = modelName
        self.storeType = type
        self.directory = directory
        self.mergePolicy = mergePolicy
        self.wipeDatabaseOnConflict = wipeOnConflict
        self.identifier = identifier
    }
    
    fileprivate func performUsingWorker(closure: @escaping ((NSManagedObjectContext) -> ())) {
        let managedContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        LegacyCoreStack.spawnedBackgroundCount += 1
        managedContext.name = "Worker \(LegacyCoreStack.spawnedBackgroundCount)"
        managedContext.parent = managedObjectContext
        managedContext.mergePolicy = NSMergePolicy(merge: mergePolicy)
        
        closure(managedContext)
    }
    
    fileprivate func loadPersistentStores(completion: @escaping (() -> ())) {
        DispatchQueue.global(qos: .background).async {
            let url = self.databaseFilePath
            
            Logging.log("Using DB file at \(url)")
            
            let options = [NSMigratePersistentStoresAutomaticallyOption as NSObject: true as AnyObject, NSInferMappingModelAutomaticallyOption as NSObject: true as AnyObject]
            let config = StackConfig(storeType: self.storeType, storeURL: url, options: options)
            
            if !self.addPersistentStore(self.persistentStoreCoordinator, config: config, abortOnFailure: !self.wipeDatabaseOnConflict) && self.wipeDatabaseOnConflict {
                Logging.log("Will delete DB")
                try! FileManager.default.removeItem(at: url!)
                _ = self.addPersistentStore(self.persistentStoreCoordinator, config: config, abortOnFailure: true)

    }
    
    public func save(completion: (() -> ())? = nil) {
        save(context: stack.mainContext, completion: completion)
    }
    
    private func save(context: NSManagedObjectContext, completion: (() -> ())? = nil) {
