Fixed a number of bugs discovered by the new testing