Refactoring JS for balance