Gemifying.
Fleshed out lib with tests and script.

Whoops, really should have committed in smaller bites;
got carried away and do not feel like cherrypicking ATM.