Lots of small error handling fixes