Fix issue with index out of range