In a stinging setback for the national gay-rights movement, Maine voters narrowly decided to repeal the state’s new law allowing same-sex marriage.

With 87 percent of precincts reporting early Wednesday morning, 53 percent of voters had approved the repeal, ending an expensive and emotional fight that was closely watched around the country as a referendum on the gay-marriage movement. Polls had suggested a much closer race.

Maine voters also decided to expand the state’s 10-year-old medical marijuana law, approving a ballot question to allow state-regulated dispensaries to grow the drug and sell it to patients. The vote came weeks after the Obama administration announced it would not prosecute patients and distributors who are in “clear and unambiguous” compliance with state laws. Maine will be the third state, after New Mexico and Rhode Island, to allow tightly regulated, nonprofit marijuana dispensaries.

With the repeal of the same-sex marriage law, Maine became the 31st state to reject same-sex marriage at the ballot box. Five other states  Massachusetts, Connecticut, Iowa, New Hampshire and Vermont  have legalized same-sex marriage, but only through court rulings and legislative action.