Refactoring js codes