Error handling fixes

Re-issuing this PR as there was another commit in there that shouldn't have been.