Fixes #8 and added a whole load of tests