Improve error msg
Improve error handling, no functional change
Fixes for #1043