refactor error detecting