js refactoring
refactor and added some docs