Began refactoring Javascript version