fix memleaks and other stuff