Some error handling and cleanup.