Clean up and add tests

Cleaned up the versioning code, extracted some additional functionality
into separate types, added unit tests and fixed a few bugs.