Ensure proper cleanup on wallet creation, opening and closing (resolves #347)