refactored JS code to use OOP and not create any globals