Minor refactoring of Javascript