Fix index calculation