Fix a memleak that's been there since 5.1.0
more misc cleanup