fix integer overflow issue for the index increment