fixed a bug when array index out of range