Fix for memory leak (and a few minor tweaks...)