Big refactoring to separate out the HTML-specific parts.