Improved error handling

Error handling was misfiring in some cases and not firing in others.
This is now fixed.