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