More memory leak fixes

More memory leak fixes and compiler warning fixes. Changes are 947eb33, d4e6377, fb7203a is a work in progress - need not pull them now.