Refactoring js, fix bugs