Fixed a bug found by the tests
Added more tests