Fixed a couple of bugs uncovered by the new tests