refactor, add tests, update readme.

Should now produce expected results on all tests.
huh, I learned something new today. I've looked at those docs so many times, never noticed that, thx