Memory leak fixes

Woowoo, finally tracked down the runaway memory leak.