Well, I'll be darned. This past Thursday, Rite Aid (RAD -51.21%) reported its first profit in five years, breaking a 21-quarter streak, and raised its full-year guidance from a possible severe loss to a possible modest profit. The stock jumped about 20% on the news, in turn breaking my resolve to maintain my underperform CAPScall on the company. Is the tide truly turning for Rite Aid?

They're doing something "rite"
Net income in the quarter came in at $0.07 per share, a stark turnaround from last year's $0.06 loss. More impressive is the fact that the company raised full-year earnings guidance from a loss of $0.09-$0.23 to a max loss of only $0.05 and a possible profit of $0.03. Even at the low end, that implies a strong improvement in the fourth quarter, historically one of the weakest.

The improvement in this quarter came almost completely as a result of increased gross margin. Admittedly, the company's poor gross margin was one of the main reasons I originally made my underperform call. I cited Change to Win Investment Group, or CtW, an investment group that works with pension funds sponsored by unions, including Rite Aid's own workers' union. The group found that Rite Aid's Wellness+ rewards program was not working as intended. According to CtW, while Rite Aid was growing its Wellness+ member base, sales per Wellness+ member had dropped 40% since the program's introduction. Furthermore, the program was intended to drive more pharmacy sales, but Rite Aid itself admitted that that wasn't happening.

Since then, gross margin has improved by 409 basis points. While sales per Wellness+ member continues to decline, the number of gold and silver members, who earn that status by spending more during the year, is apparently rising. The company hasn't given adjusted figures, but it's also probable that the decline in sales per member is unrelated -- Rite Aid closed a number of stores during the quarter, which explains some of the revenue decrease.

Far from perfect
While some of the revenue decline is due to store closures, it still must be noted that revenues did fall 1.6% during the quarter. That decline can't be completely explained with some hand-waving about store closures, as same-store sales decreased 1.5%.

However, a deeper look reveals that the decrease is largely due to a decline in pharmacy sales, which in turn is due to an increase in new generic drugs. Ironically, much of the improvement in gross margin is also due to the increase in new generic drugs, which are cheaper for stores to carry.

While pharmacy sales were down due to the lower cost of prescriptions, the number of actual prescriptions filled was up. But again, this comes with a caveat. Prescriptions were higher primarily because of a dispute between Walgreen (WBA -1.18%) and pharmacy benefits manager Express Scripts (ESRX), which sent Walgreen's customers searching for an alternative drugstore. In other words, Rite Aid got lucky. Walgreen and Express Scripts reached a new agreement over the summer, and while Rite Aid has managed to retain most of its market-share gains, it can't expect another such bounty to fall into its lap anytime soon.

Rite Aid wasn't the only company to benefit, either -- competitor CVS Caremark (CVS -0.65%) also took its share of Walgreen's customers, benefiting from the fact that 80% of CVS stores are within three miles of a Walgreen's location.

The Foolish bottom line
There's no denying that Rite Aid has managed to turn its business around. For five straight years, the company floundered and piled on more and more debt to stay afloat. Now it is turning a profit and expecting to do so again in the next quarter. But there's also no denying that Rite Aid got lucky with the release of cheaper generic drugs and the Walgreen-Express Scripts dispute, both factors that had little to do with Rite Aid's actual business performance. Whether it can hold on to those gains is far from certain, but I'm hesitant to get in front of a turnaround story that could send the stock climbing higher and wipe out any profits I made on my underperform call.