Some bugfixes uncovered by clang analyzer: allocations based on incorrect element sizes; possible double-free; (harmless) memory leaks