Added a lot of tests which pointed out some failures which I then fixed.