Refactors a bit to be more testable. Add tests.
Merge pull request #1 from marcw/tests

Refactors a bit to be more testable. Add tests.