Refactor Error Handling