Fixed bug in index calculation