Refine error handling