Refactored code to not rely on Rhino specific JS syntax