Fixing the destroy function

Including other miscellaneous fixes.