fix index calculation