Fix length calculation so it doesn't assume the length of the encoded