Fix reference count bug in insert method