fix most leaks, minor formatting