Update js code with some refactoring