refactor, add tests, update readme.

Should now produce expected results on all tests.
168de5f5