     private func mqttSubscribe(to topic: String) {
         messageId += 1
 
-        let subscribeBytes: [UInt8] = [
+        let headerBytes: [UInt8] = [
             0x82,                               // FIXED BYTE 1   8 = SUBSCRIBE, 2 = DUP QoS RETAIN
             UInt8(topic.utf8.count + 5),        // FIXED BYTE 2   remaining length, msg id + topic length + topic
             messageId.highByte,                 // VARIA BYTE 1   message id MSB
-            messageId.lowByte,                  // VARIA BYTE 2   message id LSB
-            UInt16(topic.utf8.count).highByte,  // VARIA BYTE 3   topic length MSB
-            UInt16(topic.utf8.count).lowByte    // VARIA BYTE 4   topic length LSB
+            messageId.lowByte                   // VARIA BYTE 2   message id LSB
         ]
 
         let requestedQosByte: [UInt8] = [
             0x00                                // Requested QoS
         ]
 
-        let messageBytes = subscribeBytes + [UInt8](topic.utf8) + requestedQosByte
+        let messageBytes = headerBytes + encode(string: topic) + requestedQosByte
         outputStream?.write(messageBytes, maxLength: messageBytes.count)
     }
 
     private func mqttUnsubscribe(from topic: String) {
         messageId += 1
 
-        let unsubscribeBytes: [UInt8] = [
+        let headerBytes: [UInt8] = [
             0xa2,                               // FIXED BYTE 1   a = UNSUBSCRIBE, 2 = DUP QoS RETAIN
             UInt8(topic.utf8.count + 4),        // FIXED BYTE 2   remaining length, topic id length + 4
             messageId.highByte,                 // VARIA BYTE 1   message id MSB
-            messageId.lowByte,                  // VARIA BYTE 2   message id LSB
-            UInt16(topic.utf8.count).highByte,  // VARIA BYTE 3   topic length MSB
-            UInt16(topic.utf8.count).lowByte    // VARIA BYTE 4   topic length LSB
-        ]
-
-        let messageBytes = unsubscribeBytes + [UInt8](topic.utf8)
+            messageId.lowByte                   // VARIA BYTE 2   message id LSB
+        ]
+
+        let messageBytes = headerBytes + encode(string: topic)
         outputStream?.write(messageBytes, maxLength: messageBytes.count)
     }
 

        // TODO: Add 2 (for messageId) if/when QOS > 0
        let remainingLengthBytes = encodeVariableLength(2 + topic.utf8.count + message.count)

        let publishBytes: [UInt8] = [
            0x30] +                             // FIXED BYTE 1   3 = PUBLISH, 0 = DUP QoS RETAIN
            remainingLengthBytes +              // remaining length, variable
            [UInt16(topic.utf8.count).highByte, // topic length MSB
            UInt16(topic.utf8.count).lowByte    // topic length LSB
        ]

        let messageBytes = publishBytes + [UInt8](topic.utf8) + [UInt8](message)
        outputStream?.write(messageBytes, maxLength: messageBytes.count)
    }


            keepalive.lowByte                   // VARIA BYTE 10  Keep Alive LSB
        ]

        var messageBytes = headerBytes + encode(string: client)

        if let username = username {
            messageBytes += encode(string: username)
        }

        if let password = password {
            messageBytes += encode(string: password)
        }

        outputStream?.write(messageBytes, maxLength: messageBytes.count)
