JS Code refactoring