A sweeping immigration reform bill is now headed to the Senate floor largely due to the efforts of eight Democratic and Republican senators who wrote the measure and managed to keep firm control over the debate.

The so-called Gang of Eight managed to derail dozens of proposals put forth by members of the Senate Judiciary Committee that threatened to violate the delicate trade-offs made by the bill’s authors.

The gang — the four Republicans and four Democrats who forged the immigration plan — held together “amazingly well under the circumstances,” said one member of the Judiciary Committee who was not part of the group. “It’s a very complex bill,” added Sen. Orrin Hatch, a Utah Republican.

The legislation that now goes to the Senate floor creates a 13-year road to citizenship for the estimated 11.5 million immigrants living in the United States unlawfully, establishes a new program to allow low-skilled workers into the country and sharply expands the number of visas for highly skilled workers.

It also mandates a costly new effort to secure U.S. borders against future illegal crossings and remakes the existing system for legal immigration.

Beneath the surface lie dozens of difficult political bargains meant to balance the interests of members of the self-appointed Gang of Eight and various constituencies now welded into a coalition for the bill.

Fixing the precise standards for certifying that the U.S.-Mexican border is secure enough to permit other features of the bill to take effect was one. Setting the requirements, and rights, for those illegally in the country who will apply for “registered provisional immigrant status” was another.

Only four of the eight senators, two from each political party, are on the Judiciary Committee, but aides to all met privately in advance to review roughly 300 proposed amendments. Officials said there were few disagreements among the staff about which would have violated the basic bipartisan agreement and thus needed to be fended off at all costs.

The lawmakers themselves discussed a small number in meetings held either in the office of Sen. Chuck Schumer, a New York Democrat, or John McCain, an Arizona Republican.

The results of last November’s presidential election — in which President Barack Obama won about 70 percent of the Hispanic vote — led some Republican lawmakers to support immigration reform out of concern the party was eroding support among an increasingly influential voting demographic. At the same time, the party’s conservative base has backed tough measures cracking down on immigrants living in the country unlawfully.

But even though immigration legislation is a top second-term priority for Obama, the White House was held at arms’ length in the efforts to draft the immigration legislation. Administration officials were consulted about the feasibility of quickly establishing a nationwide biometric system to track immigrants, for example, but were not invited to the meetings.

It was only by accident that the public might have learned of the gang’s power.

Speaking into a microphone that he evidently did not realize would pick up his voice, Schumer asked an aide during one vote, “Do our Republicans have a pass on this one?”

In fact, for days, the two Republican Gang of Eight members on the Judiciary Committee, Sens. Jeff Flake of Arizona and Lindsey Graham of South Carolina, wound up opposing changes they might otherwise have supported — far more often than was the case with the Democrats, Schumer and Sen. Dick Durbin of Illinois.

Among them were numerous attempts by other Republicans to toughen border security requirements before legalization can begin or to otherwise make provisional legal status harder to obtain.