Image source: Fool.com

On Thursday morning, Rite Aid (RAD 20.00%) reported a surprise profit, with EPS of $0.03, beating the average analyst estimate for a loss of $0.04 per share. That also represented a significant improvement from last year's loss of $0.05 per share in Q2.

For many years, Rite Aid has seemed to be in danger of being crushed by its larger rivals, Walgreen (WBA 3.69%) and CVS Caremark. Indeed, both competitors still pose a significant threat to Rite Aid's business in the long term. However, Rite Aid's solid performance for the past several quarters makes it seem more likely that the company will manage to overcome these competitive threats.

Margin outperformance
As has been the case for most of the last two years, margin improvement was the big key to Rite Aid's success last quarter. Revenue of $6.28 billion was up less than 1% year over year. However, Rite Aid's cost of goods sold declined more than 1%, causing gross margin to expand from 27.5% to 28.9%.

I was particularly surprised by this margin improvement because Walgreen had signaled in June that it was ramping up discounts in the front-end to drive higher customer traffic. I expected this to force at least some pricing response from Rite Aid, weighing on gross margin.

However, while Walgreen did grow sales much faster than Rite Aid last quarter, the impact on Rite Aid's gross margin was apparently minimal. Rite Aid's gross margin of 28.9% in Q2 was flat compared to Q1.

Steady improvement
Rite Aid's Q2 earnings would have looked even better except for a $62.2 million special charge that was related to debt refinancing. This was partially offset by a $23.5 million benefit related to a legal settlement, but the net effect of these two items was to depress EPS by approximately $0.04.

Rite Aid's debt refinancing last quarter was just the latest in a string of refinancing actions. Over the past year, Rite Aid has taken advantage of its improved operating results, and the low interest rate environment, to significantly reduce its interest expense. Rite Aid's interest expense in Q2 was down by more than $22 million year over year, accounting for $0.02 of the company's EPS improvement.

Threats linger
While Rite Aid's quarterly report was very positive, and the company even raised its full-year earnings guidance, management still warned that earnings would fall year over year in the second half of FY14. The press release highlighted "continued reimbursement rate pressure, pharmaceutical cost increases and a significantly lower benefit from new generics" as headwinds going forward.

Rite Aid has undoubtedly benefited from a favorable industry environment in 2012 and 2013. It's not clear, yet, whether the headwinds management cites are temporary or more permanent in nature. With lower interest expense, and a more solid base of profitability, it now seems more likely that Rite Aid will successfully navigate these upcoming challenges. Still, with the stock having nearly quintupled since late 2012, I don't think the likely rewards justify the risks of buying Rite Aid now.