     }
 
     public func diff(clientDocument: ClientDocument<T>) -> PatchMessage? {
-        let optionalShadow = dataStore.getShadowDocument(clientDocument.id, clientId: clientDocument.clientId)
-        if let shadow = optionalShadow {
+        if let shadow = dataStore.getShadowDocument(clientDocument.id, clientId: clientDocument.clientId) {
             let edit = diffAgainstShadow(clientDocument, shadow: shadow)
             dataStore.saveEdits(edit)
             let patched = synchronizer.patchShadow(edit, shadow: shadow)

    }

    public func patch(patchMessage: PatchMessage) {
        let patched = patchShadow(patchMessage)
        patchDocument(patched!)
        dataStore.saveBackupShadowDocument(BackupShadowDocument(version: patched!.clientVersion, shadowDocument: patched!))
    }

    private func patchShadow(patchMessage: PatchMessage) -> ShadowDocument<T>? {
        var optionalShadow = dataStore.getShadowDocument(patchMessage.documentId, clientId: patchMessage.clientId)
        if var shadow = optionalShadow {
            for edit in patchMessage.edits {
                if (edit.clientVersion < shadow.clientVersion && !isSeedVersion(edit)) {
                    shadow = restoreBackup(shadow, edit: edit)!
                    continue
                }
                if edit.serverVersion < shadow.serverVersion {
                    dataStore.removeEdit(edit)
                    continue
                }
                if edit.serverVersion == shadow.serverVersion && edit.clientVersion == shadow.clientVersion || isSeedVersion(edit) {
                    let patchedShadow = synchronizer.patchShadow(edit, shadow: shadow)
                    dataStore.removeEdit(edit)
                    if isSeedVersion(edit) {
                        shadow = ShadowDocument(clientVersion: 0, serverVersion: patchedShadow.serverVersion, clientDocument: patchedShadow.clientDocument)
                    } else {
                        shadow = ShadowDocument(clientVersion: 0, serverVersion: patchedShadow.serverVersion + 1, clientDocument: patchedShadow.clientDocument)
                    }
                    dataStore.saveShadowDocument(shadow)
                }
            }
            return shadow
        } else {
            return Optional.None
        }
    }

    private func restoreBackup(shadow: ShadowDocument<T>, edit: Edit) -> ShadowDocument<T>? {
        let optionalBackup = dataStore.getBackupShadowDocument(edit.documentId, clientId: edit.clientId)
        if let backup = optionalBackup {
            if edit.clientVersion == backup.version {
                let patchedShadow = synchronizer.patchShadow(edit, shadow: ShadowDocument(clientVersion: shadow.clientVersion, serverVersion: shadow.serverVersion, clientDocument: shadow.clientDocument))
                dataStore.removeEdits(edit.documentId, clientId: edit.clientId)

    }

    private func incrementServerVersion(shadow: ShadowDocument<T>) -> ShadowDocument<T> {
        return ShadowDocument(clientVersion: shadow.clientVersion, serverVersion: shadow.serverVersion + 1, clientDocument: shadow.clientDocument)
    }

    private func patchDocument(shadow: ShadowDocument<T>) -> Document<T>? {
        if let document = dataStore.getClientDocument(shadow.clientDocument.id, clientId: shadow.clientDocument.clientId) {
            let edit = synchronizer.clientDiff(document, shadow: shadow)
            let patched = synchronizer.patchDocument(edit, clientDocument: document)
            dataStore.saveClientDocument(patched)
