Fix regression introducing index out of range bug