Apply error handling patch from Matthew Willson