Fixed some memory leaks in the engine.