EAST LANSING, Mich. (AP) — For three quarters, it was a dominating performance by Michigan State — yet by the time the game ended, coach Mark Dantonio was relieved it was over.

"I am just glad I am not sitting here talking about how it slipped away," Dantonio said. "I do think you develop an identity in how you play and finish. Somehow, we finish."

The Spartans beat Nebraska 27-22 on Saturday night after leading 27-3 at the start of the fourth quarter. Michigan State's defensive front was terrific, stuffing the Cornhuskers' vaunted running game, but the Spartans allowed three touchdowns in the last 12:49. With 30 seconds remaining, Trae Waynes intercepted Tommy Armstrong's pass to halt the Huskers' final drive.

Michigan State (4-1, 1-0 Big Ten) moved up two spots to No. 8 in the AP poll Sunday after narrowly avoiding what would have been a devastating collapse. On a weekend when several other top teams lost, this escape came with a few lessons.

"In the second half, we have to play a lot better. We have to put teams away," running back Jeremy Langford said. "At some points of the game I did think it was over. We've got to get better at not letting off. We have to continue to finish strong and not worry about the score — just play the game we play."

If Michigan State had lost, special teams would have shouldered some of the blame. After Nebraska's Ameer Abdullah scored on a pair of short runs in the fourth quarter, De'Mornay Pierson-El returned a punt 62 yards for a touchdown to make it 27-22 with 3:22 remaining.

The Spartans recovered an onside kick easily enough, but Michael Geiger missed a 36-yard field goal attempt, giving the Cornhuskers (5-1, 1-1) the ball at their 20 with 1:07 remaining.

There would have been considerably less time left if not for a big mistake by the Spartans on the play before the field goal. Jeremy Langford was knocked out of bounds, stopping the clock when Nebraska had no timeouts left.

"Our guys played hard. They played hard from start to finish," Nebraska coach Bo Pelini said. "We made a couple mistakes. We gave up a couple plays. They're a good team."

Nebraska entered the game ranked No. 2 in the nation in rushing, averaging 355 yards per game on the ground. Michigan State held the Huskers to 47. The Spartans finished with five sacks.

The Cornhuskers fell two spots to No. 21 in Sunday's poll.

Nebraska had scored in every quarter this season before being blanked for the whole first half by Michigan State.

For a while, it looked as if the Cornhuskers might be shut out for the first time since 1996, but with Abdullah bottled up, Nebraska rallied through the air.

Tommy Armstrong went 20 of 43 for 273 yards. Jordan Westerkamp caught nine passes for 158 yards.

"We started well and played a tremendous football game, especially on defense for 3½ quarters," Dantonio said. "Then Nebraska caught fire a little bit and we had a missed interception. Next thing you know, they return a punt and you find yourself in a game. It is good that you have a close game. I don't like them, but it's good to have a close game and win it in the end."

Langford gained 111 yards on 29 carries, and Tony Lippett scored two touchdowns — one on a 55-yard pass from Connor Cook, and the other on a reverse.

Those big plays aside, the Spartans weren't particularly crisp on offense. Cook was 11 of 29 for 234 yards. Michigan State turned the ball over three times in its own territory in the first half, although Nebraska didn't score after any of those miscues.

"I feel defensively we took 10 steps forward. Offense, we kind of took a couple of steps back," Cook said. "A win is a win no matter how you cut it — if it's pretty, if it's ugly. We are all pretty thankful to walk away from this game."