Fix memleaks and other little things