Refactor: moved many html operations out of script to html.