         #if DEBUG
             NSLog("CocoaMQTT: UNSUBACK Received: \(msgid)")
         #endif
-
-        if let topic = subscriptions.removeValue(forKey: msgid) {
+        
+        
+        if let topicDict = unsubscriptionsWaitingAck.removeValue(forKey: msgid) {
+            let topic = topicDict.first!.key
+            
+            for (key, value) in subscriptions {
+                if value.first!.key == topic {
+                    subscriptions.removeValue(forKey: key)
+                }
+            }
+            
             delegate?.mqtt(self, didUnsubscribeTopic: topic)
+        } else {
+            #if DEBUG
+                NSLog("CocoaMQTT: UNEXPECT UNSUBACK Received: \(msgid)")
+            #endif
         }
     }
 

        let msgid = nextMessageID()
        let frame = CocoaMQTTFrameSubscribe(msgid: msgid, topic: topic, reqos: qos.rawValue)
        send(frame, tag: Int(msgid))
        subscriptions[msgid] = topic
        return msgid
    }


    open func unsubscribe(_ topic: String) -> UInt16 {
        let msgid = nextMessageID()
        let frame = CocoaMQTTFrameUnsubscribe(msgid: msgid, topic: topic)
        unsubscriptionsWaitingAck[msgid] = [topic:CocoaMQTTQOS.qos0]
        send(frame, tag: Int(msgid))
        return msgid
    }
