Indianapolis (CNN) Before he went to prison, Ernest killed his 2-year-old daughter in the grip of a psychotic delusion. When the Indiana Department of Correction released him in 2015, he was terrified something awful might happen again.

He had to see a doctor. He had only a month's worth of pills to control his delusions and mania. He was desperate for insurance coverage.

But the state failed to enroll him in Medicaid, although under the Affordable Care Act Indiana had expanded the health insurance program, making most ex-inmates eligible. Left to navigate an unwieldy bureaucracy on his own, he came within days of running out of the pills that ground him in reality.

"I have a serious mental disorder, which is what caused me to commit my crime in the first place," said Ernest, who asked reporters to use only his middle name to protect his privacy. "Somebody should have been pretty concerned."

The health law was expected to connect Ernest and almost all other ex-prisoners for the first time to Medicaid coverage for the poor, cutting expensive visits to the emergency room, improving their prospects of rejoining society and reducing the risk of spreading communicable diseases that flourish in prisons.

But Ernest's experience is repeated millions of times across the country, an examination by The Marshall Project and Kaiser Health News shows.

Most of the state prison systems in the 31 states that expanded Medicaid have either not created large-scale enrollment programs or operate spotty programs that leave large numbers of exiting inmates -- many of whom are chronically ill -- without insurance.

Local jails processing millions of prisoners a year, many severely mentally ill, are doing an even poorer job of getting health coverage for ex-inmates, by many accounts. Jail enrollment is especially challenging because the average stay is less than a month and prisoners are often released unexpectedly.

Ex-inmates with the worst chances of getting insurance and care are in 19 states that did not expand Medicaid. Only a small number qualify for coverage. Enrollment efforts by prisons and jails are almost nonexistent.

Because Henderson could leave the work-release facility to go to the doctor, the state didn't provide medical care. Federal rules say people in work release are still incarcerated.

Nationwide, 16 state prison systems have no formal procedure to enroll prisoners in Medicaid as they reenter the community, according to a survey by The Marshall Project. Nine states have only small programs in select facilities or for limited groups of prisoners, like those with disabilities. These 25 states collectively release some 375,000 inmates each year.

Failure to link emerging inmates to health insurance is a missed opportunity to improve health and save money by cutting recidivism as well as visits to the hospital emergency room, advocates say. Studies have showed Medicaid access in Florida and Washington cut return trips to jail among the mentally ill by 16 percent.

"I hate to say it -- it's a captive audience. You have somebody there! You know they're going to be released in a few weeks," said Monica McCurdy, who as head of a clinic for Project HOME in Philadelphia constantly sees homeless, recently released prisoners without Medicaid coverage. "Why not do the handoff that's needed to prevent this person winding up in the ER? It defies common sense."

Health risks soar after prison release

Before the Affordable Care Act, state Medicaid programs covered mainly children, pregnant women and disabled adults, which included only a small number of ex-offenders. That's still generally the case in the 19 states that didn't expand Medicaid.

President-elect Donald Trump has vowed to repeal the health act and replace it with something else, leaving the law's Medicaid expansion and eligibility for ex-prisoners in doubt. Rep. Tom Price, Trump's pick to head the Department of Health and Human Services -- which oversees Medicaid -- has been one of Obamacare's most vociferous critics in Congress.

But some analysts expect parts of the law to survive, perhaps including Medicaid expansion managed more directly by states than by Washington.

Even some Republicans have supported the idea, suggesting that revoking Medicaid coverage from millions of new recipients would be difficult. Republican Gov. John Kasich expanded Medicaid in Ohio in part for ex-inmates, he has said , "to get them their medication so they could lead a decent life."

Other parts of the health law received more attention, but advocates saw giving Medicaid coverage to ex-inmates as one of its most transformative aspects. Illness for illness, inmates are the sickest people in the country.

They have far higher rates of HIV, hepatitis and tuberculosis than the general population. They're also more likely to have high blood pressure, diabetes and asthma. More than half are mentally ill, according to the Bureau of Justice Statistics (PDF), with up to a quarter meeting criteria for psychosis. Between half and three-quarters have an addiction problem.

Julie Estrada-Medina, 48, a recently released prisoner, was approved for Medicaid in October. She felt that she had to choose between her sobriety "or my freedom, one of the two."

Prisons and jails have their own doctors, but their responsibility to provide care stops upon an inmate's departure. Inmates generally aren't eligible for Medicaid while imprisoned.

No time is more critical than the days immediately after release. One study showed that in the first two weeks, ex-prisoners die at a dozen times the rate of the general population. Heart disease, drug overdose, homicide and suicide are the main causes.

But even in states that expanded Medicaid, the most vulnerable and sometimes dangerous ex-inmates are often left on their own.

Ernest went to prison for shooting and killing his daughter amid a psychotic religious delusion. Re-enacting the biblical story of the sacrifice of Isaac, he thought God would intervene to save the girl. News reports from the time say police found him naked, carrying the child's lifeless body through the streets of an Indianapolis suburb.

Indiana expanded Medicaid under the health law in February 2015 and set up a system to enroll all eligible prisoners upon release. Yet when Ernest got out in August 2015, he was not enrolled in Medicaid, let alone connected to doctors.

Prison officials say they applied for Medicaid on Ernest's behalf, but Medicaid records show he applied when he got home. It's not clear where the system failed.

"It is important that the offenders have some accountability in the process," said Douglas Garrison, a spokesperson for the Indiana Department of Correction. "The IDOC has worked diligently to ensure released offenders are receiving coverage."

Ernest's letters to Medicaid and a clinic before he got out didn't help. He had to start the application process from scratch after he got home, making increasingly frantic calls and scrambling to find his birth certificate and other paperwork as his supply of lithium and perphenazine, an antipsychotic, dwindled.

"Somebody who's committed a violent felony because of a mental illness is getting out of prison, and we don't have anything set up yet?" he said.

Failure to sign up ex-inmates for health care is a common occurrence in states that expanded Medicaid under the health law, even in places such as Indiana where agencies have provided enrollment assistance.

No enrollment for thousands of chronically ill

Two-thirds of the 9,000 chronically ill prisoners released each year by Philadelphia's jails aren't getting enrolled as they leave, said Bruce Herdman, medical director for the jails. The city lacks even the $2 million necessary to supply a month's worth of medication for released inmates with prescriptions, he said.

"They give you like two weeks' supply of medication," said Ricky Platt, 49, who left the Philadelphia jail in 2015, quickly ran out of Zoloft antidepressants and became homeless. "They don't give you any resource of where to go or get a doctor and get your prescription filled or anything."

Emergency doctors at Thomas Jefferson University Hospital in Philadelphia often see released inmates with kidney failure who are at risk of dying if they don't receive dialysis almost immediately, said Dr. Priya Mammen, one of the hospital's emergency physicians.

"We're kind of the go-to spot for many people, but particularly for people who have been released from prison," she said. "Either in the first week we see them or when their prescriptions run out."

Kara Salim, 26, got out of jail in 2015. Without Medicaid coverage, she couldn't afford the fees for court-ordered therapy to treat her bipolar disorder and alcoholism.

Kara Salim, 26, got out of the Marion County, Indiana, jail in 2015 with a history of domestic-violence charges, bipolar disorder and alcoholism -- and without Medicaid coverage. As a result, she couldn't afford the fees for court-ordered therapy.

Without therapy she wasn't allowed to see a psychiatrist for her medications. Without medication she spiraled downward, eventually threatening suicide at a court hearing. When court officers tried to bring her to a psychiatric hospital, she erupted, kicking and scratching them and landing back in jail, with new felony charges: battery against a public safety officer.

"I wish I could tell you she's the exception," said Sarah Barham, an addiction counselor with Centerstone, an Indiana nonprofit.

Medicaid enrollment requires resources that many prison systems and local jails -- often overcrowded and operating in crisis mode for years -- lack or have been reluctant to commit.

"Most of the county sheriffs don't have the proper staff they need to even run the jails," said Bill Wilson of the Indiana Sheriffs' Association. Many jails are making an effort, but in some places "pulling the resources out to enroll an inmate in Medicaid is not something the sheriff's able to do."

In Minnesota, only those eligible for special release planning programs are offered assistance in applying; as a result, fewer than 1,000 of the 6,800 prisoners the state released last year applied for Medicaid, according to corrections officials there. Minnesota is one of seven states -- Alaska, Hawaii, Arizona, Montana, Louisiana and Illinois are the others -- that expanded Medicaid but have not implemented a large-scale enrollment program.

In many states, even prerelease registration requires a follow-up visit to a local Medicaid or welfare office to "activate" the coverage on release. Obtaining a phone, paying for minutes and navigating bus lines to state offices can be daunting for newly released inmates who often struggle with basic needs such as food and shelter.

Sarah Barham is an addiction counselor with Centerstone, an Indiana nonprofit. Barham says of Salim's trouble getting Medicaid, "I wish I could tell you she's the exception."

Indiana officials applied for Medicaid on behalf of more than 7,000 state prisoners from March through September -- nearly 90 percent of those released. (Many of the others were released to other states or deported, officials said.) Yet only a little more than half called to activate their coverage when they got home, according to state data. The state said in recent weeks it eliminated the requirement to activate coverage with a call.

Released prisoners also often need to reestablish identification by applying for Social Security cards and birth certificates. That can take weeks or months. Sometimes there's another step: enrolling in one of the private, managed care networks that many states hire to administer Medicaid benefits.

In the chaotic days and weeks after release, red tape can mean the difference between joining Medicaid or remaining cut off from community caregivers.

William Santee, 46, released from Pennsylvania state prison this year, has diabetes, high cholesterol and high blood pressure. He learned about Medicaid enrollment requirements and the need to visit a welfare office from workers at a homeless shelter.

The prison "didn't tell me about where to go or anything like that," he said. "They don't consider that their responsibility." Waiting in line and completing the welfare-office paperwork took five hours.

Getting the details right

Almost as critical as successful enrollment is choosing a Medicaid plan that covers medicines and services ex-inmates need. Jail and prison workers are rarely equipped to wade through such details.

"That's a huge issue for us," said Susan Jo Thomas of Covering Kids and Families, a nonprofit that helps enroll people in Medicaid in Indiana. "You finally get a person to the place where they are ready ... to go into detox, but if they have aligned with an insurance company that doesn't cover the medicine that program uses, then you have a problem."

In some extreme cases bureaucratic rules clash, leaving ex-prisoners stranded between agencies. In Indiana and several other states, corrections departments consider prisoners in work release programs, who report to jobs during the day, to be free. That means they're not eligible for care from the prison system.

Medicaid, on the other hand considers them still incarcerated. So they can't enroll in community health coverage, either.

"We got all excited when Obamacare came out, because everybody's going to be covered," said Peggy Urtz, who runs an Iowa work release facility for women. Instead, she said, the women "are going to ERs when they're ill and racking up medical bills. We have good providers, well experienced in working with women, and they can't go to them because they don't have insurance."

A few states and localities reap praise for innovative and comprehensive attempts to enroll emerging prisoners in Medicaid.

Ohio recently finished phasing in Medicaid registration at all state prisons and is one of the few states giving inmates a managed-care insurance card as they leave, said John McCarthy, that state's Medicaid director. Chicago's huge Cook County jail puts prisoners on the Medicaid books as they enter, rather than before they leave, to sidestep the common problem in jails of unpredictable release dates.

Join the conversation See the latest news and share your comments with CNN Health on Facebook and Twitter.

More often the process looks like what was happening one recent Friday in Indiana's Marion County jail, where Lt. Debbie Sullivan was trying to rouse sleepy women to sign up for health insurance.

The document she distributed was three pages long, authorizing a Medicaid application on inmates' behalf. It asked for names, addresses, birth dates and Social Security numbers. The handwritten information would later be entered into computers -- a recipe for transposed digits and misspelled names.

"The program remains a work in progress," said Katie Carlson, a spokeswoman for the Marion County Sheriff's Office, which runs the jail. "It has proven a daunting task to enroll, track and provide meaningful information on both Medicaid and health care."

Experts say such sessions require a half hour or more to get the details right and answer questions about picking the right plan and following up with doctors and insurance officials after release.

Sullivan's knowledge of the women's next steps was minimal. In response to questions, she simply told them to contact their local social service office when they get out. She walked out of the block with about 30 signed applications. It was over in 15 minutes.

"Thank you ladies!" she called on her way out, as the heavy steel door slammed behind her.