Derry's hopes of reaching the first round proper of the UEFA Cup were given a major boost tonight when they beat Gretna 5-1 away from home.
Ryan McGuffie had given the Borders club an early lead but that was cancelled out by Darren Kelly.
Kevin Deery and Ciaran Martyn then helped themselves to a double each to put the Irish club well and truly in the driving seat ahead of the second leg at Brandywell in a fortnight's time.
Derry wasted no time putting the Gretna defence under pressure in the opening minutes.
John Deery squared across the face of goal, searching for O'Flynn at the back post, but Martin Canning managed to intercept the move and play the ball out for a corner.
At the other end of the park, a blunder from Derry goalkeeper David Forde could have gifted Gretna the opener but they failed to seize upon the opportunity and Ryan McGuffie's feeble effort fell wide.
However, it was McGuffie who provided the opener after just 12 minutes, when he pounced on a Brendan McGill header before sending a low drive home.
Derry were back on level terms after 23 minutes when Sean Hargan raced down the left flank before picking out Darren Kelly for the header and he managed to squeeze the ball home at the near post.
Townsley was fortunate to avoid at least a yellow card when he appeared to use his elbow on Gary Beckett — but he escaped unpunished.
And Forde was equally lucky not to hand Gretna a goal shortly before the break. In his haste to race from his line to make a clearance, the Derry goalkeeper fell on his backside and could only breath a sigh of relief when Steve Tosh smashed the ball into the sidenetting.
Gretna manager Rowan Alexander took advantage of the break to swap McGill for John O'Neil.
The first booking of the match came eight minutes after the restart when Chris Innes was cautioned for tripping Ciaran Martyn.
And the foul proved to be costly when Kevin Deery sent a superb, right-footed effort into the top corner of the net from the resultant free-kick with 54 minutes gone.
Before Gretna even had time to rethink their gameplan, Derry — and Deery — had the ball in the back of the net again two minutes later.
This time the ball broke to him 35 yards out and he proved to be just as clinical from that range when he sent a ferocious half-volley past Main.
The Gretna goalkeeper was left picking the ball out of the net again after 63 minutes.
Martyn raced into the box, rounded the keeper and squeezed the shot between the two helpless defenders in the goalmouth.
If Gretna were still harbouring any hopes of salvaging this tie in the return leg at Brandywell in a fortnight's time, they were dashed when Martyn grabbed his second of the night.
It was another opportunistic effort and once again Main was beaten as the Derry midfielder unleashed a stinging drive from the edge of the area with 15 minutes remaining.