Fix bugs in ToC generation (unclosed tags, etc)