Hopefully this mostly speaks for itself. I also did a bit of cleaning up of the error handling. Fixes #8047