Gemify

Hi there. I made a gem out of your script. 

When doing so I also split it to functions and such so it is easier to understand and somewhere more performant.

Not sure if you will like it though. In case no, I can revert the split and just use the original script in the binary.