fixed bug in encode/decode, thanks Jonathan