Fixed the index calculation