Update script.js

Refactored code, added more comments