Fixed index out of bounds bug