Better atom content handling and fix bad/broken HTML

This version parses the HTML instead of simply checking for balanced opening and closing tags.