Generate final XML. Working version