Spain boss Fernando Hierro admitted after the 2-2 draw with Morocco that his side were lucky to finish top of World Cup Group B and stressed the need for defensive improvement.
Hierro's men were set to finish second in the pool, and face Group A winners Uruguay in the last 16, until a stoppage-time equaliser by substitute Iago Aspas.
Initially the flag was up for offside, but the goal, Spain's second leveller of the contest, was subsequently given following a VAR review.
With a late Iran goal seeing them hold Portugal 1-1, Spain and Portugal ended up level on five points in first and second place respectively, with goals scored putting the former ahead.
And, thus, it will be hosts Russia for La Roja in the last 16 while the Portuguese take on the Uruguayans.
Hierro, having succeeded the sacked, Real Madrid-bound Julen Lopetegui on the eve of the tournament, has overseen a 3-3 draw with Portugal, a 1-0 win over Iran, and now this result.
And he said in his post-match press conference, streamed on FIFA's website: "This entire group was a tough one and history bore it out.
"So the first conclusion I draw is that despite all these challenges, we emerged on top, so we need to look at the silver lining, think positive.
"Obviously we can improve and there are things that definitely need to be worked on.
"We will definitely be self-critical - (conceding) five goals in three matches is not the way forward, and it's what I've told my players."
We need your consent to load this rte-player contentWe use rte-player to manage extra content that can set cookies on your device and collect data about your activity. Please review their details and accept them to load the content.Manage Preferences
Spain had gone 1-0 down in the 14th minute when Khalid Boutaib punished a mix-up between Andres Iniesta and Sergio Ramos, before drawing level five minutes later through Isco's strike.
They survived scares after that when Boutaib almost took advantage of further slack defending as he was denied by David De Gea, and Nordin Amrabat's shot hit the post, before Youssef En-Nesyri put Morocco 2-1 up in the 81st minute.
Hierro added: "I have to say that we were lucky to finish first.
"I'm going to repeat that our initial goal was to be at the top of the group and we've reached that goal.
"And now we need to be very demanding with ourselves and have very clearly in mind that if we concede so easily, it will be very difficult for us to reach our ultimate goal."
Morocco boss Herve Renard, whose side finished bottom of the group on one point having also lost 1-0 to Iran and Portugal, saw them collect six yellow cards in this match.
He took issue with Spain's Gerard Pique escaping a booking for an early challenge on Boutaib, and also questioned the corner that led to Aspas' goal.
Renard said in his press conference: "We were a bit upset when Pique was not booked when he used both feet, but maybe this is not the most important aspect.
"The question I'd like to raise is on the second equaliser - we expect a corner on one side and it's played on the other. I wonder whether this is allowed. If not, there's a major mistake. I tried to check with the referee, but the door was closed."
He added: "We would have loved to have beaten Spain - that was why we came here. We suffered, like any other team who takes on Spain.
"I think our players should be commended. The whole team should be commended for this World Cup.
"We've showed that we can take on two of the best teams in the world, Portugal and Spain."