Library split

As discussed, a split into a library, an executable and a test suite. Most of the executable code I've taken off Main so that it can be referenced by the test suite.