Working refactor of JS to cinlude functions and push it to JS file