fix the index out of bound bug