improve error handling (based on Ryan's feedback)