Small javascript refactoring