Slimmed down code and fixed tests