Fix for issue #1606 and #1508 with jruby and JRE 1.7

Also brings the code inline with tenderlove's original gist.