reorganize code from html to js file