fix index calculation bug