Aron Gunnarsson scored twice as Cardiff secured a win over Barnsley in a thrilling game in the Championship.
Kenny Miller opened the scoring for Cardiff on 10 minutes before substitute Joe Mason's rebound made it 2-0.
Danny Drinkwater pulled one back from a free-kick only for Gunnarsson to volley Cardiff two goals ahead once more.
Second-half goals from Don Cowie and Gunnarsson looked to have wrapped up the win but Jimmy McNulty and Ricardo Vaz Te goals set up a nervy finale.
Cardiff made a lively start with Gunnarsson firing over the crossbar before Ben Turner tested Luke Steele from long range.
And it was the home side who took the lead on 10 minutes with the returning Miller slotting home following Cowie's pass.
Cardiff continued to control the game and an instinctive save from Steele denied Miller's powerful header following Peter Whittingham's corner.
Miller's involvement was soon to end as he was forced off the field with a head injury after colliding with team-mate Turner, with Joe Mason the replacement.
Mason was in the thick of the action almost immediately and the former Plymouth striker was fouled on the edge of the area by Rob Edwards.
Whittingham's free-kick was parried by the keeper before Mason followed up to score with rebound.
Within two minutes Barnsley has pulled a goal back with former Bluebird Drinkwater's long-range free-kick took a deflection past David Marshall.
However during a frantic spell Cardiff re-established their lead within two minutes with Gunnarsson scoring with a stunning strike on the volley from 20-yards following Whittingham's cross-field pass.
Drinkwater, who spent last season on loan at Cardiff, struck the bar with a free-kick shortly after the interval but it was the home side who extended their lead on the hour mark.
Cowie latched on to Mason's through-ball before rounding Steele to make it 4-1.
Iceland international Gunnarsson added a fifth from close range after collecting Andrew Taylor's pass in the 71st minute.
It seemed Cardiff had done enough to secure the victory but they had to withstand a late Barnsley fightback.
McNulty headed home from Jacob Butterfield's right-wing cross in the 82nd minute before Vaz Te scored their third four minutes later when he fired home from substitute Craig Davies' lay-off.
But Malky Mackay's side held on to end their three-game winless run in the Championship.