Fixes to error handling, other improvements