CONCORD, N.H. (AP) — Republican and Democratic lawmakers in the New Hampshire Senate reached agreement late Thursday on a bill to ban gay conversion therapy on minors after a debate over religious freedom nearly derailed the effort.

“Who among us would want to be converted from the essence of who we are? I don’t think any of us would,” Republican Senate Majority Leader Jeb Bradley said during debate.

Gay conversion therapy is the practice of trying to change someone’s sexuality or gender identity. Both the Senate and House have now approved a bill barring licensed counselors from engaging in the practice with anyone under age 18. The chambers passed bills including slightly different language, which means they’ll need to reach agreement before sending the legislation to Gov. Maggie Hassan’s desk.

Hassan, a Democrat, praised the Senate’s passage of the bill, saying it sends an important message to young people that they can be who they are.

The Senate’s passage of the bill comes after two weeks of emotional debate. The measure looked certain to fail earlier Thursday, when lawmakers tabled it after being unable to reach agreement over language regarding religious freedom. The final bill says people licensed to provide counseling services under state law, from nurses to marriage counselors, can’t engage in the practice. It also includes language saying the law cannot infringe on religious freedom, intended to ease concerns that the bill would prevent priests and other religious leaders from talking to teenagers about their sexuality.

Democratic Sen. David Pierce, who is gay, made several personal appeals to his colleagues to back the ban. Pierce said he realized he was gay at age 11 and struggled to accept it. Research shows gay and lesbian teenagers are more likely to commit suicide than their peers and the risk increases if they undergo conversion therapy, Pierce said.

“I worked my way through it, but there was no person sitting next to me telling me I was sick and needed to be cured,” he said.

The American Psychological Association and other major health organizations have discredited gay conversion therapy and states are beginning to pass bans on the practice for minors. California, New Jersey, Oregon, Illinois and Washington, D.C., ban the practice for minors and Vermont is likely to join them. But efforts in Hawaii and Colorado failed this year.

Republicans who opposed the bill expressed concern that it would prevent priests from counseling teenagers.

The bill passed on a voice vote after a key amendment cleared with support of all 10 Democrats and six Republicans.

© 2016, Associated Press, All Rights Reserved.

This material may not be published, broadcast, rewritten, or redistributed.