Add specs for everything

This pull request involves a fairly large amount of actual code refactoring, because the code was not quite *testable*.

Now the code is testable **and** it has tests!