Add tear down function. Fixes #4458