Fixes a bunch of bugges thanks to a bunch of new tests