An execution in Oklahoma went disastrously wrong on Tuesday night, when a state corrections department doctor injected death row inmate Clayton Lockett with a secret and untested chemical cocktail that was supposed to kill him quickly and painlessly. About 15 minutes into the execution, it became gruesomely clear to observers that Lockett was conscious, seizing, and in what appeared to be tremendous pain. Officials halted the execution, but 43 minutes after he had been first injected, Lockett died of a heart attack. A second man who was to be executed the same night, Charles Warner, has been granted a stay of execution for two weeks.

Oklahoma was using the experimental formula because pharmaceutical companies increasingly refuse to supply "safe" lethal injection chemicals. That's left capital punishment states to choose between executing inmates under dangerous conditions or not executing them at all. Many states have chosen to go ahead, and some have adopted secrecy laws that shield the chemical compounds used for the executions.

Drug companies stopped supplying the key lethal injection chemical in 2011

The key chemical in lethal injections is sodium thiopental, originally invented as an anesthetic. But US manufacturers of the drug have been increasingly refusing to sell it, either out of opposition to the death penalty or concern about association with executions. In 2011, the last US supplier, a company called Hospira, stopped making it.

Later that year, the European Union announced an export ban on sodium thiopental, in pursuit of its official goal of "universal abolition" of the death penalty. Belarus, a pro-Moscow dictatorship, is the only European country that retains the death penalty; ending capital punishment worldwide is a major political issue in many European countries. Hospira may have shut down production in 2011 in part because it had come under pressure to do so from the government of Italy, where it has a plant.

Sodium thiopental has a shelf-life of only four years, meaning that it cannot be stockpiled beyond 2015. The result is that US prisons are running out of the key ingredient for lethal injections.

Capital punishment states had to find another way to make lethal injections

State governments that wanted to continue using lethal injections were left with few options. Some imported sodium thiopental illegally from Europe, but federal and European officials appear to have shut down those trades. Some tried importing them from other exporters, but there is not a lot of overlap between countries that produce complex anesthetics and countries that retain the death penalty. One of the few, India, sold a few batches to South Dakota and Nebraska before Indian officials banned the sales.

That left capital punishment states with two final options: they could mix legally available drugs themselves, creating their own ad hoc lethal injections, or they could pay compounding pharmacies to do it for them (compounding pharmacies combine or mix custom drugs, and face little government regulation for small-batch jobs). In effect, they wanted to make up new lethal injection cocktails. But without a way to do rigorous testing before using the drugs, the execution room effectively became the test lab; death row inmates were also lab rats.

The new lethal injections opened states up to legal challenges

The US constitution forbids cruel and unusual punishment. The legal standard this sets for the death penalty, under current US law, is that an execution method has to be "safe," which means that it does not inflict too much pain.

The problem with using untested drug combinations invented by loosely regulated compound pharmacies or by a state doctor's guesswork is that the state cannot prove that they won't cross this legal pain threshold. Exacerbating the problem, a series of botched lethal injections in 2011 and 2012 did fail to painlessly and immediately kill the inmate, causing an obviously high degree of pain in the process.

This allowed lawyers who defend death row inmates to argue that the new compounds crossed the "cruel and unusual" threshold. In some cases won, courts ruled that the new combinations were unfit for executions. States had given anti-execution lawyers a new and often successful legal argument.

States tried to solve this problem by turning to secrecy

States such as Oklahoma have responded to the legal challenges by passing secrecy laws that allow it to keep the details or origins of their lethal injections a secret. Some states have even passed laws requiring drug suppliers to keep secret about the sales. Officials in Oklahoma have even taken to using petty cash when they purchase individual drugs for the cocktail in order to cover their tracks. The idea is to make it harder for lawyers to challenge the legality of their lethal injections by simply hiding the details.

The secrecy has, unsurprisingly, invited a whole new set of legal challenges. Those challenges have actually succeeded: in March, a state court ruled that the secrecy law was unconstitutional, and the state supreme court ordered a stay of the executions until the legal issues could be resolved.

Oklahoma officials defied the court rulings

Three days after the ruling, Oklahoma revealed the combination of drugs it would use: midazolam, pancuronium bromide, and potassium chloride. But they didn't reveal the reasons for choosing those drugs or where they'd bought them from. As Andrew Cohen wrote at the Atlantic, "no other state that has ever used midazolam to execute a man has ever used it in the dosage now suggested by Oklahoma. So not only are the origins and efficacy of these drugs unknown but there is no record suggesting one way or the other whether the combination Oklahoma plans for Lockett and Warner will inflict unconstitutional pain." State officials would say only that the drugs had been obtained legally from licensed pharmacies and had not expired.

The state supreme court's stay of execution became a big political issue in Oklahoma, with Republican Gov. Mary Fallin ordering that executions to proceed as scheduled. The state supreme court and the state criminal appeals court argued over jurisdiction, with the latter at first announcing it had overturned the former's decision, and then the state supreme court again announcing a stay of execution. Conservative state lawmakers angrily criticized the decision, with one announcing he would seek to impeach the judges. It looked headed for a full-on constitutional crisis, with the governor set to defy the state supreme court. But the state supreme court ruled last Wednesday that it had deemed the secrecy law constitutional and that the execution could proceed.

This is happening more and more

Oklahoma's case is far from unique. A staggering seven percent of lethal injections are botched, often resulting in grisly and painful incidents like that on Tuesday night.

As stockpiles of sodium thiopental have thinned or expired, executions have decreased, but so have states become more reliant on ad hoc cocktails and little-regulated compounding pharmacies to provide injections for the executions that do go ahead. Episodes like Tuesday night's are now just a feature of our continued use of capital punishment.

In January, Oklahoma used pentobarbital acquired from an officially secret source to inject death row inmate Michael Lee Wilson. "I feel my whole body burning," Wilson said during the procedure, before dying in apparent agony. That episode, like this one, provoked horror around the country and sparked a national debate about the growing problem of unsafe lethal injections. But that did not stop it from happening again.