Fix out of bound (ref after clear) bug