Updated error handling to use Error class