restructure javascript