Fix memory corruption found by unit tests.