Fix memory management and stuff.