Rewrote the Ruby code

On my machine, execution dropped from 256s to 49s