trying to fix memleaks. Not done yet.