     (while (zero? (.get payload (dec (.position payload))))
       (.position payload (dec (.position payload))))
     (let [trimmed-payload-size (.position payload)]
-      (.aset-byte packed (.byteValue (new Long trimmed-payload-size)))
+      (aset-byte packed (byte-to-long (.byteValue (new Long trimmed-payload-size))))
 
       ; now copy the array from the payload to the packed array.
       (java.lang.System/arraycopy (.array payload) 0 packed 6 (.position payload))

   Ignore every other byte. Go to Payload size state when a start byte is seen;
   don't save the byte regardless. When a start byte is seen, completely reset
   the state."
  [{:keys [decode-sm ^ByteBuffer input-buffer] :as channel} a-byte]
  (when (or (= a-byte MAVLINK1-START-BYTE)
            (= a-byte MAVLINK2-START-BYTE))
    (reset! decode-sm {:state payload-size-state
                   :protocol (if (= a-byte MAVLINK1-START-BYTE)
                               :mavlink1
                               :mavlink2)})
    (.clear input-buffer)
    nil))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   to the appropriate next state depending on the protocol."
  [{:keys [decode-sm ^ByteBuffer input-buffer] :as channel} a-byte]
  (.put input-buffer ^byte a-byte)
  (reset! decode-sm (if (= (.get input-buffer 0) MAVLINK1-START-BYTE)
                              seq-id-state
                              incompat-flags-state))
  nil)

(defn start-state
