major refactoring to JS, clean up the code and made it modular