fixed invalid length calculation