fix memleaks and other little things