JS refactoring?