Improved refactoring to JS