Cristiano Ronaldo scored twice to become the first player to find the net in a fourth UEFA European Championship, and, more important, he kept Portugal alive in Euro 2016 after a thrilling 3-3 draw with an already qualified Hungary in Lyon, France.

Ronaldo netted both goals in the second half, in each case after Portugal had fallen behind to leave its hopes of progressing hanging by a thread. The first goal was a brilliantly improvised flick of a João Mário cross that fell just behind him in the penalty box. When Portugal’s continued openness at the back soon allowed Balázs Dzsudzsák to get his second goal of the game and put Hungary in front for a third time in a frenetic contest, Ronaldo once more provided the response. This time it was a Ricardo Quaresma cross that set him up, as a superb leap and pinpoint header by the Real Madrid star did the damage.

Incredibly, Portugal continued to allow Hungary chances to grab a winner that would have ousted the Iberian country from the competition. But, with no further troubling of the scorers, Portugal sneaked into the last 16 as one of the four best third-place teams after Iceland secured second place behind Hungary with a last-gasp winner against the now-eliminated Austria.

It means that while Hungary awaits the runner-up in Group E, Portugal will now face a last 16 meeting with Croatia. Although that would not have been an ideal scenario at the start of this competition, the fact Portugal is still alive at all comes as a considerable relief after its opening three performances.

In a group that appeared set to be dominated by Portugal and Austria, it has proved to be anything but. And there was nothing predictable about the final round of games. With a place in the knockout phase already assured after a win over Austria and a draw with Iceland, Hungary made five changes to its side. And rather than greatly diminish its quality, the alterations appeared to freshen up its play.

At 19 minutes, midfielder Zoltán Gera struck a superb opening goal, chesting down 25 yards out and sending a perfectly executed low shot arrowing into the bottom corner of the net. At that point, Portugal, fresh from the frustration of a draw with Austria in which Ronaldo missed a penalty, seemed heading for the earliest of exits, just as at the 2014 FIFA World Cup.

But just before halftime, Portugal’s star began to shine. Three minutes before the interval, Ronaldo played a sublime slide rule pass to get Nani in behind the defense, and the winger beat veteran goalkeeper Gábor Király with a first-time shot.

The drama had only just started, though. Less than two minutes after the second half got under way, Hungary was back in front, with Dzsudzsák curling a left-footed free-kick that got a crucial deflection off André Gomes in the wall to wrong-foot goalkeeper Rui Patrício. Portugal failed to heed the lesson of that goal, when eight minutes later allowing the winger to cut inside on his dangerous left foot and fire another shot that again got a deflection, this time off the outstretched boot of Nani on its way into the Portuguese net.

It took Ronaldo goals No. 7 and No. 8 in European Championship competition to keep Portugal alive, but Fernando Santos’ side will surely have to function a whole lot better from top to bottom to go much further in this competition.