         
         if (status == errSecDuplicateItem) {
             do {
-                return try update(value: value, forEntry: entry)
+                return try update(value: value, forKey: key)
             } catch  {
                 throw WMFKeychainCredentialsError.unhandledError(status: status)
             }

        return value
    }
    
    fileprivate func deleteValue(forEntry entry:String) throws {
        let query = commonConfigurationDictionary(forEntry: entry)
        let status = SecItemDelete(query as CFDictionary)
        guard status == noErr || status == errSecItemNotFound else { throw WMFKeychainCredentialsError.unhandledError(status: status) }
    }
    
    fileprivate func set(value:String?, forEntry entry:String) throws {
        // nil value causes the entry to be removed from the keychain
        guard let value = value else {
            do {
                try deleteValue(forEntry: entry)
            } catch  {
                throw WMFKeychainCredentialsError.couldNotDeleteData
            }
            return
        }
        
        var query = commonConfigurationDictionary(forEntry: entry)
        let valueData = value.data(using: String.Encoding.utf8)!
        query[kSecValueData as String] = valueData as AnyObject?
        query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlocked

        case unhandledError(status: OSStatus)
    }

    fileprivate func commonConfigurationDictionary(forKey key:String) -> [String : AnyObject] {
        return [
            kSecClass as String : kSecClassGenericPassword,
            kSecAttrService as String : Bundle.main.bundleIdentifier as AnyObject,
            kSecAttrGeneric as String : key as AnyObject,
            kSecAttrAccount as String : key as AnyObject
        ]
    }

    fileprivate func getValue(forKey key:String) throws -> String {
        var query = commonConfigurationDictionary(forKey: key)
        query[kSecMatchLimit as String] = kSecMatchLimitOne
        query[kSecReturnData as String] = kCFBooleanTrue
        
