Added a custom error type and some error handling