Fixing memory leaks and other monstruosities