Refactor error checking utils