refactor, add tests, update readme.

Should now produce expected results on all tests.
don't worry about it. i can easily squash when i merge ;)