fix bug in length calculation