Fixed some issues with memory leaks in code.