Refactor error checking.