Fixed index out of bounds error