Refactor error handling to be more clean and succinct