Code refactoring to the js file