Tham Luang caves, Thailand (CNN) The Wild Boars are back. Twelve young boys and their soccer coach are safe, freed from the floods deep inside a cave in northern Thailand where they had been trapped in for more than two weeks. The mood in Thailand's Chiang Rai province is elated.

When a Thai military unit had finished dismantling a section of water pumps that had proved so crucial in the successful operation, they jumped into their open-topped vehicle near the Tham Luang cave on Wednesday, smiling triumphantly to the crowd of cheering supporters who had stayed on to give their heroes a proper send off.

But behind those jubilant scenes lay the knowledge that each one of those rescue workers carries: the near three-week operation had rested on a knife's edge, with equal chance of success or disaster.

Now, with the mission over and the boys recovering in the hospital, the full story of the remarkable rescue can be told.

The early challenges

In the days after the boys first went missing on June 23, rescuers had no idea where they were, or if they were still alive. Expert divers from the Thai Navy SEALs, experienced in conventional diving situations, were faced with an unprecedented challenge.

Speaking to CNN on Wednesday, Thai Navy SEAL commander Rear Adm. Arpakorn Yookongkaew recalled those initial difficulties.

Rain and the long winding caverns of an unfamiliar cave were already a concern. On first assessment of the scene, "our hope to find the boys was less," he said.

Thai military personnel inside the cave during the rescue operations.

At that time, the task must have seemed hopeless. But that didn't stop local officials from mobilizing a team of 110 Thai SEALs, hundreds of soldiers and other volunteers, determined first to find the boys and then, somehow, bring them back.

Yookongkaew's team navigated through the pitch-black caverns and under rising, murky water. Eventually, they found footprints at a T-junction, giving them hope the team were near. However, rising waters prohibited the SEALs from further exploration. Industrial-scale pumps were brought onto the site in an effort to draw some of the water out.

But they only helped to draw a centimeter or two of water each day, said Yookongkaew.

Decisions made early by the Thai rescuers proved crucial in opening the chance for the rescue to take place: one was a huge operation to pump out water from the cave, and another was to block all the streams that carried water in from the outside.

Thousands of miles away, on the other side of the world, Bill Whitehouse was at home in rural Derbyshire, England, cooking a lunch of bacon and eggs, when the phone rang. The subject of the call was not a complete surprise. Whitehouse, a retired cave diver and vice chairman of the British Cave Rescue Council, was aware of the Thai rescue effort.

Vernon Unsworth, an experienced British caver who now lives in northern Thailand not far from the Tham Luang cave, had already alerted his contacts back in the UK to the predicament faced by the Thai Navy SEALs. The cave diving community is small, and the number of people capable of carrying out complex operations such as the one required to find and free the Wild Boar soccer team, smaller still.

So when the call came through, Whitehouse and his colleagues in the British Cave Rescue Council sprang into action.

With his wife away looking after the grandchildren, he abandoned his lunch and found himself helping to orchestrate an international response in the heat of an unusually blistering British summer.

The Thai Embassy in London secured three seats on the next Thai Airways flight to Bangkok; John Volanthen and Rick Stanton, an IT consultant and a former firefighter who just happened to be two of the most experienced cave divers in the world, were dispatched to London's Heathrow Airport along with a cave diving veteran, Robert Harper.

Three British cave-divers, Rick Stanton, Robert Harper and John Volanthen arrive at Khun Nam Nang Non Forest Park near the Tham Luang cave in Chiang Rai on June 27, 2018.

After they landed in Bangkok, and had taken the short internal flight north, the trio took an exploratory dive. But the flooding had become far worse at that point. They were confronted by the enormity of the task.

"At that stage there was no way they could get anywhere near the boys. Conditions were really quite bad, the water flow was very strong and the suspended sediment made visibility very low. To be fair, the chances of getting far were not high," Whitehouse said.

It had been more than a week and there still had been no contact with the boys

'Like driving in fog with your headlights on'

It wasn't until the water started to clear a couple of days later that water levels started to drop and visibility improved. Along with two Australians, the British divers conferred with the Thai SEALs and mapped a route.

"Whenever cave divers go into a passage for the first time they will use a guideline and use their finger and thumb to make sure they have a route mapped out," Whitehouse explained.

"Underwater, it's completely dark. You have lots of lights on your head but there's a lot of mud and suspended sediment. You can't see much. It's like driving in fog with your headlights on."

But it wasn't until their third attempt at penetrating the caves, on July 2, that Volanthen and Stanton stumbled upon the boys. Volanthen had reached the end of his guide line, secured it in the muddy waters, and emerged to an astonishing sight.

Members of the Royal Thai Navy are pictured with the 12 schoolboys, members of a local soccer team, and their coach, who were trapped in the Tham Luang Cave network in Northern Thailand.

Facing him, on a ledge about a meter above the water, were 12 boys and their coach. "How many of you," he asked them. "Thirteen," one of the boys answered.

"Brilliant," replied Volanthen.

It was an incredible moment, Whitehouse said.

"The only thing is that once we found them, we began thinking, now what?"

Yookongkaew said the original plan was to keep the boys and their coach inside the cave for a month or more while rescue workers found a way to bring them out, perhaps via another entrance or until the water levels had dropped.

But the group were perched on a small muddy ledge 4 kilometers (2.5 miles) inside the cave complex, surrounded by floodwater and with a limited supply of oxygen, and it was depleting fast.

Running out of time

A diving option was always going to be risky. Divers on site had described the conditions as some of the most extreme they have ever faced, and the boys didn't know how to swim, let alone dive.

"The boys weren't going to stay alive in the chamber. But if you try a risky operation and they die, would you have been better to leave them there and hope the waters go down?" Whitehouse said.

As rescuers pondered the best course of action, four Thai divers remained with the boys, stabilizing their health and gradually reintroducing them to food.

The decision to move the boys wasn't taken lightly. It was complicated by the July 6 death of former Thai Navy SEAL Saman Kunan, who ran out of air while returning from an operation to deliver oxygen tanks to the cave.

His death changed the mood on the ground, highlighting just how dangerous the mission had become.

Yookongkaew called Kunan "a hero for Thai and the rest of the world."

Former Thai Navy SEAL Sgt. Saman Kunan died while returning from an operation to deliver tanks to the cave.

But there was little time to grieve the loss.

Monsoon rains were an impending threat, and it had become clear that there was no other way to bring the children out.

Whitehouse arranged for two more cave divers,Chris Jewell and Jason Mallison, supported by three cave rescuers, to go to Thailand with new equipment. He sourced child-sized diving masks and some harnesses from a firm in France. One UK supplier drove from outlet to outlet to get whatever was needed after realizing it had little left in stock.

Meanwhile in Thailand, divers practiced parts of the operation with local schoolchildren in a nearby swimming pool.

Then, on Sunday July 8, 13 foreign divers, including a doctor , joined five Thai Navy divers for the first rescue attempt.

The boys were given 5-millimeter-thick wetsuits, full face mask breathing apparatus and air bottles while other divers remained dotted throughout the other chambers in case any of the boys were struggling.

The boys were split into groups of four. Each boy was taken out by two divers, who carried their oxygen tanks and guided them through murky tunnels. Each rescue took several hours, with much of the time spent under water.

The most dangerous part of the journey out of the cave system was the first kilometer (0.62 miles) during which they were required to squeeze through a narrow flooded channel.

Having completed this section, the boys were then handed over to separate, specialist rescue teams, who moved them through the remainder of the cave, much of which they could wade through.

That same process was repeated on Monday and Tuesday, until the entire team and their coach were brought to safety.

A helicopter believed to be carrying one of the boys rescued from the flooded caves.

There are reports that the children were sedated during the operation. Officials are cagey about that.

"We had to use the means that could keep the children not to be panicky while we were carrying them out," Yookongkaew said, adding that he wouldn't be adding any details about that part of the operation other than it was a joint decision.

"Most importantly, they are alive and safe," he said.

Now, the boys are in an isolation ward in hospital, gradually regaining their strength. Over the next few days, when doctors are confident they are free of infection, they will be able to talk to their parents in the same room. In the meantime, as a global effort disbands and exhausted rescuers head home, Thailand is celebrating an outcome that feels little short of miraculous.