PITTSBURGH (AP) -- Pittsburgh Pirates general manager Neal Huntington spent the hours leading up to the non-waiver trade deadline searching for a way to upgrade the team with the best record in baseball. The typically buttoned down Huntington even considered taking leave of his senses to do it. ''We talk a lot about, we don't want to do something stupid,'' Huntington said Wednesday after the deadline passed. ''We were willing to do something stupid. We just didn't want to do anything insane.'' At the moment, insanity might be defined as breaking up the chemistry the Pirates have spent the last four months cultivating. Russell Martin drove home Neil Walker with the go-ahead run in the eighth inning and the Pirates rallied past the St. Louis Cardinals 5-4 to extend their lead in the NL Central to 2 1/2 games. Pittsburgh (65-42) has its best record entering August since the 1972 Pirates ended July at 60-35. Martin's sharp grounder off Trevor Rosenthal (1-2) rolled into left field, giving Walker time to score from second and propel Pittsburgh to its 25th comeback win in an increasingly special season. Pittsburgh is 23 games over .500 for the first time since 1992. ''We can win just about every way possible,'' manager Clint Hurdle said. The Cardinals appeared in firm control of the division race two weeks ago but now find themselves staring up at the Pirates, who have taken the first four games of a five-game series. St. Louis broke out of a slump to put together 13 hits, but the Cardinals left 11 runners on base and dropped their seventh straight. The Cardinals led 2-0, 3-1 and 4-2 but couldn't hold on. ''You always at some point during the season, every year, no matter what, go through a rough patch,'' St. Louis starter Adam Wainwright said after failing to earn his 14th victory. ''The good teams find a way to get out of that rough patch and find a way to get back to playing good quality baseball, and that's what we're going to do.'' Tony Watson (3-1) worked two shutout innings in relief of starter Jeff Locke. Mark Melancon pitched a perfect ninth for his fifth save. Melancon is 3 for 3 in save opportunities since All-Star closer Jason Grilli went down with a right forearm injury last week. Matt Holliday had three hits and drove in two runs but St. Louis failed to score over the final five innings against Pittsburgh's bullpen. ''There's something we're not doing, and we know we're not doing it and fixing it,'' Cardinals manager Mike Matheny said. Walker hit his seventh homer of the season off Wainwright in the first inning, starting a pattern that repeated itself throughout the night. The Cardinals found ways to score off starter Jeff Locke, but Pittsburgh kept chipping away. ''It's things we've done throughout the course of the season,'' Pittsburgh center fielder Andrew McCutchen said. ''If we're down early, we just keep working.'' The teams with the two best records in the National League were mostly spectators before Wednesday's non-waiver trade deadline, though the Pirates acquired minor leaguer Robert Andino from Seattle. The past two seasons, the Pirates made somewhat aggressive moves meant to bolster their playoff chances, but instead contributed in part to a second-half swoon. This time, Huntington is keeping the group he called ''one of the tightest'' he's been around intact. When the 4 p.m. deadline passed, the Pirates were huddled around a TV in the clubhouse. They weren't anxiously waiting for news. Instead, they were watching Martin and first baseman Garrett Jones play video games. ''The trade deadline is like a gossip magazine,'' Watson said. ''You take it for what it's worth but it's in one ear and out the other.'' Pittsburgh needed to find a way after a rare off-night by Locke, whose rapid ascension from fifth starter to All-Star has fueled Pittsburgh's relentless pursuit of the Cardinals. But St. Louis spent four innings pecking away at the left-hander's usually deft mix of breaking balls. The Cardinals came in hitting just .155 (30-194) during their late-July swoon but peppered Locke for 10 hits, the most he has given up in 31 career starts. They came in various ways, from a hard-hit double by Beltran in the fourth to a swinging bunt by David Descalso that traveled 20 feet. Locke tied a season high by giving up four runs. He struck out six and walked one as his ERA rose from 2.15 to 2.36. ''He was fighting uphill all night,'' Hurdle said. Wainwright, however, couldn't take advantage of the first signs of life by the St. Louis offense in a week. Every time the Cardinals would push in front, the Pirates would respond, eventually tying it on a sacrifice fly by McCutchen in the fifth. Wainwright left after seven innings, allowing four runs on eight hits, striking out six and walking one. ''(They) gave me the lead three times and I blew the save three times,'' Wainwright said. ''I can be better than that, I will be better than that.'' NOTES: The Pirates placed reserve C Mike McKenry on the 15-day disabled after he underwent surgery on Tuesday to repair a torn meniscus in his left knee. Rookie Tony Sanchez, Pittsburgh's top pick in the 2009 draft, will serve as the primary backup to Martin for the rest of the season ... The series concludes Thursday with Pittsburgh's Charlie Morton (3-2, 3.59 ERA) facing Joe Kelly (1-3 3.44). The Pirates are 5-1 in Morton's last six starts.