Fix some bugs using brand new unit tests