Add ability to clean up resources when iteration ends early