Minor refactoring to use original javascript code.