Tidied up a bit and added some test output stuff.