Editors Note: This story is the result of a two-year CNN investigative report into peace talks held between the Libyan Islamic Fighting Group (LIFG) and the Libyan Government which recently culminated in the LIFG, a militant jihadist group once close to Osama bin Laden, repudiating al Qaeda. "The Jihadi Code," a documentary on the breakthrough against al Qaeda in Libya, airs on November 15 at 1200 GMT.

Tripoli, Libya (CNN) -- From within Libya's most secure jail a new challenge to al Qaeda is emerging.

Leaders of one of the world's most effective jihadist organizations, the Libyan Islamic Fighting Group (LIFG), have written a new "code" for jihad. The LIFG says it now views the armed struggle it waged against Col. Moammar Gadhafi's regime for two decades as illegal under Islamic law.

The new code, a 417-page religious document entitled "Corrective Studies" is the result of more than two years of intense and secret talks between the leaders of the LIFG and Libyan security officials.

The code's most direct challenge to al Qaeda is this: "Jihad has ethics and morals because it is for God. That means it is forbidden to kill women, children, elderly people, priests, messengers, traders and the like. Betrayal is prohibited and it is vital to keep promises and treat prisoners of war in a good way. Standing by those ethics is what distinguishes Muslims' jihad from the wars of other nations."

The code has been circulated among some of the most respected religious scholars in the Middle East and has been given widespread backing. It is being debated by politicians in the U.S. and studied by western intelligence agencies.

In essence the new code for jihad is exactly what the West has been waiting for: a credible challenge from within jihadist ranks to al Qaeda's ideology.

While the code states that jihad is permissible if Muslim lands are invaded -- citing the cases of Afghanistan, Iraq and Palestine -- the guidelines it sets down for when and how jihad should be fought, and its insistence that civilians should not be targeted are a clear rebuke to the goals and tactics of bin Laden's terrorist network.

CNN was given exclusive access to the Abu Salim jail where the code was written to talk to the LIFG prisoners. The jail has a bloody reputation; in 1996 prison guards put down a revolt by allegedly killing more than 1,200 prisoners in less than 24 hours. Read how CNN got inside the prison

We also had exclusive access to the story behind the new code from two of its principle architects.

When Saif al Islam al Gadhafi, the son of Libyan leader Muammar Gaddafi, decided he wanted to open a dialogue with the LIFG he needed to convince them he was genuine so he sought out a former LIFG commander Noman Benotman, who was living in London.

The younger Gadhafi convinced Benotman he would free LIFG members from jail if they renounced their long war with the regime. He promised Benotman immunity from prosecution and in January 2007 flew him back to Libya to meet with the LIFG leaders in the high-security Abu Salim jail.

Benotman and the other leaders in the LIFG had fought together in Afghanistan in the early 1990s helping the Afghan Mujahedeen overthrow the Soviet-backed government in Kabul. During those years they'd come to know bin Laden and many other of al Qaeda leaders.

Although they'd been brothers in arms with bin Laden, the LIFG never merged its operations with al Qaeda due to differences in approach. In particular the Libyan group never endorsed bin Laden's global jihad, preferring to concentrate their attention on overthrowing the Gadhafi regime and replacing it with an Islamic state. From the mid-1990s the Libyan Islamic Fighting Group's Afghan-trained fighters waged a fierce insurgency against the Libyan regime.

In a face-to-face meeting in 2000 Benotman warned bin Laden not to attack the United States because any gains would be outweighed by the inevitable retribution, and undermine his group's efforts in Libya. By then Libyan security services had arrested many of the group's fighters in Libya.

Despite their differences bin Laden respected the LIFG's leadership. Indeed, according to Benotman, in the years before 9/11, bin Laden wanted to use the LIFG's extensive global network for his own ends but the group refused to put its assets at al Qaeda's disposal.

When Benotman met with the LIFG leaders in January 2007 it was the first time they'd seen him in years. Some had been in jail for more than a decade. Others were captured in the international dragnet for jihadists thrown out by security services in the wake of 9/11. They agreed to consider Saif al Islam's proposal but had demands of their own.

They wanted greater freedoms in jail, the right to consult with their rank and file membership, access to religious research books and more. Over time and many meetings the security officials granted most of their demands.

Saif al Islam was motivated not just to bring a formal end to the civil war but to put a stop to al Qaeda's growing influence in Libya.

As recently as 2006 al Qaeda documents captured by U.S. forces in Iraq showed per capita more Libyans than any other Arab nation were joining al Qaeda's fight. The regime's fear was that they'd bring their fight back to Libya.

In late 2007 as Benotman, the LIFG leadership and Libya's security officials debated the way forward al Qaeda tried to derail the peace process. Bin Laden's deputy Ayman al Zawahiri issued a statement declaring the LIFG had joined al Qaeda.

Benotman fired back an open letter to Zawahiri questioning his credibility. "I questioned their idea of jihad ... directly you know. This is crazy, it is not Islamic and it's against the Sunni understanding of Islam," Benotman told CNN. Zawahiri chose not to respond. As late as this August Zawahiri's video statements included praise of LIFG leaders, in what may have been a desperate attempt to head off the condemnation he could see coming.

Even so progress in the jail was slow. In April 2009 the talks were nearly derailed when Ibn Sheikh al Libi, a prominent jihadist was found hanged in his prison cell. According to sources familiar with the talks, Saif al Islam feared his death, which some LIFG members considered suspicious, could put the whole process in jeopardy. He put pressure on prison officials to meet the LIFG's remaining demands, giving them greater freedom to consult with the rank and file.

That led the LIFG to start writing their Revisons. In July 2009 the peace process received a boost when 30 LIFG members living in the UK, some of them senior figures in the group, signed on. The group's UK members, some of whom were under British government Control Orders because they potentially posed a danger to UK national security, had previously been openly skeptical of the talks.

In September the new code, the "Corrective Studies", was completed, resulting in scores of lower and mid-level LIFG members being freed. Moammar Gadhafi's son says the group's leaders will be released at some point in the future, and will be encouraged to educate and dissuade Libya's youth from going off to fight with al Qaeda.

According to Libyan sources, the British domestic intelligence agency MI5 passed on the Revisions document, after it was published, to a number of individuals subject to Control Orders in the UK. Those sources say the peace process led to Control Orders being dropped against 11 members of the group living in the UK, leaving only one Libyan subject to the restrictions. The process initiated in Tripoli appears to have directly made the West safer.

In their new code for Jihadists, LIFG's leaders made it clear that battling extremism will be challenging. "We have written this book knowing full well that the old motives and ideas which made us take up the armed struggle in the past are still to be found in the hearts and minds of many young Muslims today," they wrote.

"We know there are many issues that might lead them to take the same path; that's why we are offering our advice and guidance to these brothers."

Given its credibility and the fact that several other prominent Jihadists in the Middle East have turned against al Qaeda, the LIFG's about face may be an important step toward staunching al Qaeda's recruitment.