Lots of small fixes in error handling