Fix a possible index out of range error