Fix bug in array index calculation