moar JS refactoring