Fixed a bug in index calculation