Updated next index calculation to be more intuitive.