some bug fixes in insert/save documents when internals and references is present