Some work on tests and things like checkstyle

Adds tests for new code. Also includes some refactoring. Now with more
reusable chunks of functionality! Most things are mockable now and there
is very little difused knowledge of internal implementation details.