Refactoring JS for clarity