Fix issue with index out of bounds