Reworked decoding logic and fixed tests