with the changes of the new webservice method set_relations these changes are necessary to keep documents related (related to #4005 internal). Also some code cleanup an minor optimization