Code refactoring of JS