fixed index out of range bug