Refactor to handle failures