WIP: Memory leaks and Fixes suggested by @pv.