Ryan Sidebottom took seven wickets as Yorkshire claimed a first-innings lead against Middlesex - but the hosts could have been in an even stronger position at Scarborough had it not been for John Simpson.
Wicketkeeper Simpson made 110 as the visitors recovered from a precarious 11-4 to make 232 in reply to Yorkshire's total of 253 on day two of the LV=County Championship Division One match.
Veteran left-arm paceman Sidebottom gave a magnificent exhibition of the art of swing bowling to pick up 7-44 from 17 overs, the second best figures of his career.
He claimed three early victims to leave Middlesex reeling before Simpson came in to combine with Dawid Malan in a 168-run stand that put the game back in the balance.
Yorkshire eventually resumed on 211-6 after a wet outfield had delayed play from getting underway on time, then lost their last four wickets for the addition of 42 runs in 55 minutes.
Tim Murtagh and Steven Finn picked up two wickets apiece, the latter getting former England team-mate Tim Bresnan caught for 20 to wrap up the innings and finish with 4-81.
Sidebottom didn't take long to strike with the new ball as his eighth delivery accounted for Chris Rogers, then he dismissed Eoin Morgan, who failed to trouble the scorers, and Paul Stirling straight after lunch.
However Malan and Simpson dug Middlesex out of a hole, the former contributing 78 from 150 balls until Sidebottom had him caught in the slips with the score on 179.
Simpson went on to complete a superb century and had struck 17 fours and a solitary six - the maximum coming off leg-spinner Adil Rashid - by the time he became the eighth wicket to fall, run out by Rashid's throw.
Murtagh became Sidebottom's final wicket as Yorkshire grabbed a 21-run advantage. The White Rose county were able to extend that cushion to 50 by the close, though not before they lost day one centurion Adam Lyth for nine.