2nd try at pull #2205
Merge pull request #2212 from WalterBright/refactor_error_check2

2nd try at pull #2205