refactored out code common to the JS and Gosu impls