General cleanup and error handling.