cleanup reorg and fix some memory leaks

Change-Id: I787916de193ff0d1bc805d14ac0e2ba018ebcbbc
Merge topic 'leaks_and_cleanup' into master

b90516f6 cleanup reorg and fix some memory leaks