Refactored code using jslint.com