Fixed sometimes using illegal array index caused by bad math.