fix index out of bound bug