Fix memleaks, minor cleanups