Cleanup and error handling.