Revert "Several bug-fixes, ready to port to C++?"

This reverts commit bac76afdc1d0b19e9fd643cc1118cb56b3664ecb.