Bug fix in index calculation