Version 2 of the script. Implemented JS Object Model, completely refactored code and cleaner variable scope