length-1 changed to length