Errors

Better error handling in general, and factored out the extension logic.