fix the index calculation error