script.js refactoring