Refactor error state handling