factor out length calculation