Fix numerous regressions that were exposed by --disable-stats, both in the core library and in the tests.