This reverts commit 27c9e8b4. Bugs exposed by AddressSanitizer have been reproduced and fixed locally: * commit e37000f3 * commit 435d44bf