a little JS refactoring to make it nicer.