Fix index out of bounds panic