Refactor generate_html and improve tests.