Fixed valgrind errors in the engine.