Portugal survived a scare and a missed Cristiano Ronaldo penalty as they beat Latvia 4-1 in Sunday's World Cup qualifier.
William Carvalho's first international goal and late efforts from the captain and Bruno Alves rescued the victory after Arturs Zjuzins had shocked Estadio Algarve with a well-taken equaliser midway through the second half.
Ronaldo had put his side ahead from the spot just prior to the half-hour mark, but the Real Madrid star hit the post with a second spot-kick eight minutes before Latvia - who had lost back-to-back games against the Faroe Islands and Hungary - scored their surprise leveller.
The European champions had been kept at arm's length for much of the match despite dominating possession, with the fluid attacking display that saw them score six against both the Faroe Islands and Andorra sorely lacking.
Carvalho proved an unlikely hero with a header from Ricardo Quaresma's cross, before Ronaldo's emphatic volley and a late Alves header ensured that Fernando Santos' side moved back into second place in Group B, three points behind Switzerland.
Portugal unsurprisingly began in dominant fashion without putting the Latvia goal under undue threat and Igors Tarasovs missed the first presentable chance as he turned a corner straight into Rui Patricio's grasp.
Glebs Kluskins was fortunate to escape with only a yellow card for a high challenge on Andre Gomes and Portuguese frustrations grew when the Barcelona midfielder was himself booked for a late tackle on the Latvia forward two minutes later.
The visitors had contained Portugal efficiently, but they ruined their good work after 28 minutes. Nani was felled in the box by the sliding Gints Freimanis after a clever one-two with Ronaldo and the captain struck the penalty beneath Andris Vanins' grasp.
Portugal continued to be frustrated by Latvia's deep-lying defence despite enjoying close to 75 per cent possession.
A clumsy challenge from Kluskins on Gomes gave Ronaldo the chance to score a second spot-kick after 58 minutes, but his low strike rattled Vanins' right-hand post before deflecting off the goalkeeper's back and out of play.
Just eight minutes later, the home crowd was stunned into silence as Latvia snatched an equaliser with their first shot on target, with Zjuzins blasting the ball beneath Patricio from the edge of the area after Portugal had failed to clear.
Any Portugal anxiety lasted only two minutes, though. Substitute Quaresma bent a fine cross in from the right and Carvalho met the ball ahead of Vanins to head in from inside the six-yard box.
Gelson Martins dragged an effort wide and Ronaldo saw a close-range strike blocked, but the 31-year-old was not to be denied five minutes from time as he met Quaresma's deep cross with a splendid scissor-kick volley that went through Vanins' legs for his seventh goal in three international games.
Ronaldo - who now has 68 international goals, level with Gerd Muller and Robbie Keane in the all-time European standings - hit the crossbar with a header before Alves nodded in Raphael Guerreiro's cross to add some gloss to the scoreline and ensure a welcome three points in Portugal's quest to reach Russia 2018.