Call cleanup before throwing error. Fixes #40