Refactoring javascript and html code