Various minor fixes, and improved error handling.