(CNN) — (CNN) -- President Barack Obama fought back and Republican challenger Mitt Romney mostly stood his ground.
The second of three presidential debates on Tuesday night brought the desired energy from the president sought by worried Democrats after a lackluster performance in the first encounter nearly two weeks ago.
A forceful Obama defended his policies and challenged Romney on shifting positions on key issues while arguing his Republican rival's proposals would favor the wealthy if elected in three weeks.
Romney repeatedly attacked Obama's record, saying millions of unemployed people and a sluggish economic recovery showed the president's policies had failed.
"Most improved -- that award goes to Barack Obama," said CNN Senior Political Analyst David Gergen. "I think he had a much stronger debate tonight."
Romney also performed well for a second straight debate, Gergen said, adding that Obama got the edge on Tuesday night.
A snap CNN/ORC International poll showed 46% of respondents thought Obama won, compared to 39% for Romney. The result was within the survey's margin of error.
Both candidates walked the floor with microphones in hand at Hofstra University in Hempstead, New York, raising their voices at times and repeatedly challenging each other's points during the 90-plus minute debate that featured a town hall-style format.
Moderator Candy Crowley, the CNN chief political correspondent, tried in vain at times to prevent each candidate from going over allotted time, with Obama speaking for more than three minutes longer than Romney on the night.
Obama was on the attack from the start, but waited until his final answer -- with no chance for Romney to respond -- to raise his opponent's controversial "47%" comments at a fundraiser in May.
In remarks made public by a secretly recorded video of the event, Romney described 47% of the country as people dependent on government aid who refused to take personal responsibility.
"Think about who he was talking about," Obama said, listing people on Social Security "who've worked all their lives," veterans "who've sacrificed for this country," students, soldiers and "people working hard every day."
The president said he wanted to fight for those people "because if they succeed, I believe the country succeeds."
Earlier, Obama went after Romney's five-point economic plan that the GOP candidate repeated two times on Tuesday night, saying it really was a one-point plan "and that plan is to make sure that folks at the top play by a different set of rules."
Romney shot back that "if you elect President Obama, you know what you're going to get -- you're going to get a repeat of the last four years."
Over and over, Romney repeated what he called the failings of Obama's policies including rising federal deficits and debts, more than 20 million people unemployed and anemic economic growth.
"We don't have to settle for what we're going through," Romney said at one point. "We don't have to settle for gasoline at four bucks. We don't have to settle for unemployment at a chronically high level. We don't have to settle for 47 million people on food stamps. We don't have to settle for 50 percent of kids coming out of college not able to get work. We don't have to settle for 23 million people struggling to find a good job."
Obama was "great as a speaker, but his policies don't work," Romney said. Attempting to rebut Obama's criticism of his policies, Romney insisted he would prioritize middle class growth, saying "it's about how we can get the middle class of this country a bright and prosperous future."
However, Romney failed to provide further specifics of his tax policy, even when one audience member asked about unspecified deductions and loopholes the candidate says he will eliminate.
On a sensitive foreign policy topic, the candidates clashed at the front of the stage over the September 11 terrorist attack in Libya that killed four Americans, with Romney suggesting the Obama administration played politics by failing to immediately acknowledge what happened.
Obama shot back that the suggestion anyone in his administration would play politics on such an issue was "offensive." When Obama said he called it a terrorist attack shortly afterward, Romney challenged him, and Obama responded "check the transcript."
Crowley cut in to say both men were right -- Obama referred to terrorism shortly after the attack, but the administration took longer to fully explain what occurred.
Unlike the first presidential debate, the town hall-style format allowed audience members to ask the questions. Crowley, the first woman to serve as moderator of a presidential debate in 20 years, tried to get in as many questions as possible from the uncommitted voters in the hall, sometimes struggling to cut off the candidates as they tried to make points or argued with each other.
The first question came from a 20-year-old college student, worried about whether he'd be able to support himself after graduation.
"More debt and less jobs. I'm going to change that. I know what it takes to create good jobs again," Romney said, addressing the first-time voter. "When you come out in 2014 -- I presume I'm going to be president -- I'm going to make sure you get a job."
Later questions addressed immigration, with Romney saying Obama failed to deliver on a promise to pass a major immigration overhaul while the president said Romney backed conservative positions such as opposing a measure that gives some children of unregistered immigrants a path to legal status.
Obama repeated his past criticism that Romney's tax plan doesn't add up, saying "when he's asked how are you going to do it, which deductions, which loopholes are you going to close, he can't tell you."
"You're going to be paying for it," Obama said of middle class taxpayers. "You're going to lose some deductions, and you can't buy the sales pitch. Nobody who's looked at it that's serious actually believes it adds up."
Romney, meanwhile, repeatedly insisted his plan won't raise taxes for middle class Americans but also won't raise or lower the tax burden on the wealthy, in contrast to Obama's proposal to let tax rates on the top 2% of wage earners return to higher tax rates from the Clinton era.
The president needed a strong debate to try to blunt Romney's rise in the polls since their first showdown in Denver, which analysts and polls indicated the GOP challenger won a clear victory.
The most recent CNN "poll of polls" -- an aggregate of the latest major surveys -- showed Romney with a slight edge nationally at 48%-47%. In the battleground states considered up-for-grabs, polls show Romney has narrowed Obama's lead or caught the president just three weeks before the election.
The Obama campaign conceded he had a bad night in the first debate and promised a more aggressive approach in New York. A third and final debate focusing on foreign policy will take place October 22 in Florida.
Polls show voters consider the economy to be the most important election issue.
Unemployment fell below 8% in September for the first time since the month Obama took office in 2009. However, continuing high unemployment and slow growth have made the president vulnerable.
Romney and his campaign have sought to frame the election as a referendum on Obama's presidency, citing joblessness, slow recovery from the recession and chronic federal deficits and debt as reasons to deny a second term.
For their part, Obama and Democrats have tried to make the election about competing visions for the future. They argue Republican proposals to repeal major legislation, such as health care and Wall Street reforms, while cutting government and expanding tax cuts without identifying additional revenue sources would stall a sluggish but steady recovery.