Improved error handling, cleanup.
Improved error handling, cleanup.