Refactoring JS codes