Handle thrown exceptions in callbacks better.

Ensure that `cleanup` runs before the callback is invoked, to prevent for example a thrown exception in the callback preventing the cleanup from happening.
Release 0.8.2