Even if the US military has finally found the right formula for the war in Afghanistan, implementing it will still be a huge struggle.

That much was clear as the top US military officer checked on the war’s progress last week at bases scattered across the baking desert of southern Afghanistan and on an aircraft carrier in the Gulf of Oman.

The trip by Adm. Mike Mullen also included stops in Iraq and Pakistan, illustrating the vast scale of the US military undertaking that followed the Sept. 11, 2001, attacks and the problems that remain for Washington, nearly nine years later.

The US military has learned painful lessons during that time, especially in Iraq, and it is eager to apply them to its renewed push to defeat the Taleban in Afghanistan with tens of thousands of extra troops.

“We’re the best counterinsurgency force in the world and we learned that through blood and treasure,” Mullen, chairman of the Joint Chiefs of Staff, told Reuters in an interview on Saturday as he flew home.

“We know what we need to do. I think we know how to do it. It’s now a matter of resourcing it and executing it.” Many factors, however, remain outside the control of the troops and their commanders — including the reliability of the Afghan government and the political mood back home.

US military officers say they did not have enough troops to stabilize Afghanistan as insurgent violence soared over the past few years while Washington was focused on Iraq.

Now they are adding more forces and say they understand they must win the support of ordinary Afghans — by providing protection from Taleban militants, working closely with tribal elders and creating access to government services.

Mullen pushed that theme in town-hall style meetings in Afghanistan with junior troops, dwelling in particular on the importance of avoiding civilian casualties.

Aboard the USS Ronald Reagan last Monday, as fighter jets roared off the flight deck, officers told reporters traveling with Mullen that ground forces and pilots had developed closer collaboration to avoid harming civilians.

A sign on the wall at the headquarters hut of US Marines at Camp Leatherneck in southern Afghanistan reads simply: “Mutual Respect and Cooperation.”

Mullen told troops at Afghan bases they represented the shift in military focus ordered by President Barack Obama, who has declared that Afghanistan must be stabilized to prevent Al-Qaeda re-establishing safe havens there.

The United States is adding more than 30,000 troops to Afghanistan to reach 68,000 later this year, on top of some 36,000 from mainly NATO allies. The Pentagon also installed US Army Gen. Stanley McChrystal, an intense and driven special operations forces officer, in mid-June as the top US and NATO commander in Afghanistan to shake up the war effort.

In a move to demonstrate US goodwill toward local communities, Mullen flew by helicopter through the spectacular mountain scenery of the Panjshir Valley to attend the opening of a girls’ school on Wednesday.

The school, right up against a rocky hillside, was built by a charity run by Greg Mortenson, an American who has helped set up some 130 schools in Afghanistan and Pakistan. He was once critical of the military but now advises top officers.

“Many of our commanders ... really get it,” said Mortenson, dressed in traditional Afghan clothes including a woolen brown cap and sitting at a desk in the stone-walled schoolhouse as curious children looked on.

“It’s about building relationships, listening more and having respect,” said Mortenson, author of a best-selling book about his experiences in the region called, “Three Cups of Tea.” US Marines in southern Afghanistan stressed that approach in Operation Strike of the Sword, launched earlier this month to secure towns in the Helmand river valley. They consulted in advance with local leaders and encountered little resistance.

NATO troops have cleared areas of Helmand of insurgents before, only to see the Taleban return when they leave. This time, they insist, they will stay until Afghan authorities can assume control of administration and security.