Remove a bunch of unused code and fix some leaks