refactored script.js and errors fixed