Afghans are slightly more optimistic about the future than they were last year, despite a stagnant economy and near-constant attacks by a revitalized Taliban , according to the results of a nationwide poll released Tuesday.

The annual survey by the San Francisco-based Asia Foundation, released in Kabul, found that 32.8 percent of Afghans believe their country is moving in the right direction, up from 29.3 percent in 2016. Another 61.2 percent said the country is heading in the wrong direction, down from 65.9 percent — a record high — in 2016.

The foundation acknowledged that the slight increase in optimism is “difficult to explain.”

The country has been mired in war since the 2001 U.S.-led invasion. The Taliban have regrouped and driven the Afghanistan’s beleaguered security forces from a number of districts across the country. An upstart Islamic State affiliate has meanwhile carried out several attacks targeting civilians.

The foundation polled 10,012 Afghan men and women in face-to-face interviews conducted between July 5 and July 23 in all 34 provinces. The poll has a 1.4 percent margin of error.

The findings marked the reversal of a decade-long downward trajectory, the foundation said. However, most respondents expressed concern about the security and future of the country, and 38.8 percent said they would leave Afghanistan if they had the opportunity, the second-highest number recorded since the survey began in 2004.

“This year’s data reflects a rise in optimism despite the challenging security environment and lack of employment,” Abdullah Ahmadzai, the foundation’s Afghanistan country director, was quoted as saying.

“Educational development, agricultural development, good security, and the building of roads and bridges are frequently cited as things that are going well at the local level,” he said.

He said confidence in public institutions has slightly improved, though nearly all Afghans say the country’s rampant corruption affects their lives, consistent with last year’s findings.