Mainlt unittest fixes, with some module fixes for good measure.

Please see individual commit messages for more detail :D