Off-spinner Saeed Ajmal took all five wickets as Pakistan finished on top on the second day of the second Test against South Africa at Newlands on Friday.
South Africa were struggling at 139 for five at the close, still 199 runs behind Pakistan's first innings of 338. Ajmal took five for 41.
Three of Ajmal's wickets came after the use of the decision review system, including a controversial dismissal of star South African batsman Jacques Kallis.
South African captain Graeme Smith and Hashim Amla were both out leg before wicket after being given not out by umpire Steve Davis.
Kallis was given out caught at short leg and immediately called for a review, which showed that the ball had not touched his bat before looping off his pad to the fielder.
But he was then given out leg before on the advice of television umpire Billy Bowden, although replays showed the ball was only brushing the outside of his leg stump, in the area known as "umpire's call".
Kallis was clearly shocked and stood at the wicket seeking clarity before reluctantly returning to the pavilion.
It was not clear what the conversation between Davis and Bowden had been and on what basis Kallis was given out on what was a marginal call.
Controversy apart, it was a superb display of bowling by Ajmal, who bowled 25 overs unchanged into a stiff south-easterly wind, varying his flight and mixing off-spin with cleverly disguised top spinners and "doosras" which turned away from the right-handed batsmen.
It was a productive day for Ajmal, who helped fellow tailender Tanvir Ahmed put on 64 for the ninth wicket to take Pakistan comfortably past the 300 mark.
Their eventual total seemed unlikely when Vernon Philander took three wickets in his first three overs to reduce the tourists to 268 for eight.
Ajmal came on to bowl after 11 overs had been bowled in South Africa's innings and struck with his ninth delivery, Smith missed a sweep and Pakistan successfully sought a review.
Two overs later Ajmal had Alviro Petersen smartly caught at short leg by Azhar Ali, who dived to his right to hold a sharp chance.
At tea, Ajmal had taken two for 20.
Amla went back on his stumps and was also given out on review before Kallis was dismissed.
He was followed by Faf du Plessis, caught at slip off a "doosra" for South Africa's highest score of 28.
AB de Villiers and Dean Elgar survived until the close.
Pakistan batted until lunch after resuming at 253 for five, although Philander's early strikes raised the home side's hopes of wrapping up the innings quickly.
Philander, who took five for 59, struck with his first ball of the day when Asad Shafiq was caught at first slip without adding to his overnight score of 111. Shafiq batted for 308 minutes and faced 230 balls.
Sarfraz Ahmed, the other overnight batsman, fell for 13 in Philander's next over when Petersen dived full length to his right to hold a good catch at third slip.
Umar Gul was leg before wicket without scoring as Philander completed his ninth five-wicket haul in Test matches.
But Tanvir and Ajmal went for their strokes, with Tanvir in particular hitting out aggressively as he made 44 off 59 balls with four fours, while Ajmal made 21 not out.
South Africa suffered a blow when fast bowler Morne Morkel left the field after bowling three balls of his 21st over with what was described as a tight left hamstring.
Tanvir fell shortly before lunch when he hit left-arm spinner Robin Peterson to Philander at deep mid-off, with last man Mohammad Irfan following in the next over when he was bowled by Peterson, going for a big hit.