Refactored a bit to work more nicely with JRuby.