fixed unbalanced tree in document.write