Fix XML-generation: When creating XML-elements, it must (should) be using the same Document-instance. See also #28. Also makes sure the XML generation and clearing of data elements is done synchronously.