Add better introspection, move cleanup to a finally: clause