As my frequent readers know, I've been a big skeptic about Rite Aid Corporation (NYSE:RAD) for the past couple of years. Rite Aid does not have a strong historical track record of profitability, and it comes in a distant third to Walgreen (NASDAQ:WBA) and CVS Caremark among U.S. drugstores. I expected these issues to catch up with Rite Aid eventually.

However, I was wrong. Rite Aid just reported a second straight annual profit, and it is forecasting solid earnings growth for the year ahead. Even with some sales metrics remaining weak, Rite Aid has boosted its margins through drug-sourcing efficiencies.

Rite Aid has achieved steady margin improvement in the last few years.

A recent drug distribution agreement with McKesson (NYSE:MCK) will provide additional benefits later this year. As a result, Rite Aid's recent turnaround seems sustainable. As the company works to continue growing earnings and reducing debt, its stock could keep moving higher.

Bouncing back from a troubled history
Rite Aid started racking up losses even before the Great Recession, and it was still losing money until 2012 -- long after most other retailers had recovered.

RAD Normalized Diluted EPS (TTM) Chart

RAD Normalized Diluted EPS (TTM) data. Source: YCharts.

Rite Aid's return to profitability was facilitated by two events. First, Walgreen had a temporary dispute with a major pharmacy benefits manager during 2012. This forced a large number of Walgreen's customers to fill their prescriptions at other pharmacies. Rite Aid picked up a portion of these customers, helping to drive a 3.4% increase in its same-store prescription count during its 2013 fiscal year.

Second, Rite Aid and other pharmacy chains benefited from a big wave of new generic drugs that began in 2012 and continued into 2013. Generic drugs carry significantly higher profit margins than brand-name drugs. This helped drive gross margin (excluding LIFO charges related to inventory valuation) up from 26.7% in FY 2012 to 28.2% in FY 2013.

Sustaining the strong performance
Even just a few months ago, it wasn't clear that Rite Aid's profit improvement was sustainable. Walgreen was regaining market share at Rite Aid's expense, and the wave of generic drug introductions was slowing.

Indeed, Rite Aid's same-store prescription count fell 0.3% last year, and lower traffic also led to a 0.2% decrease in front-end (i.e., non-pharmacy) same-store sales. However, Rite Aid offset this relatively weak sales performance with another increase in gross margin, from 28.2% to 29.1%.

Rite Aid grew earnings last year despite losing some market share to Walgreen.

Rite Aid's gross margin performance was remarkable because the company faced two headwinds in the back half of FY 2014: fewer new generic drug introductions and a highly promotional sales environment for the front end. According to Rite Aid's management, purchasing efficiencies related to generic drugs helped the company weather that storm.

Rite Aid's recent deal for McKesson to source all of its generic drugs should further reduce the company's costs, creating an opportunity for additional margin expansion. As one of the biggest buyers of generic drugs, McKesson can take advantage of its scale to negotiate better prices with manufacturers.

Additionally, Rite Aid will not have to carry as much inventory under its deal with McKesson. This reduces its working capital, and will provide a one-time boost to free cash flow this year of about $150 million. This will allow Rite Aid to pay down high-cost debt, saving money on future interest payments while also improving its credit profile.

Foolish bottom line
While Rite Aid's management is starting to focus on growth opportunities rather than remaining fixated on turnaround issues like margin improvement, Rite Aid is not in the clear just yet. Walgreen and CVS still have better real estate footprints, more financial resources, and more scale, making them formidable competitors.

That's why I'm still not looking to jump into Rite Aid stock. However, if you are a risk-tolerant investor looking for a turnaround play, Rite Aid could have more upside. If Rite Aid can continue to widen its margins through efficient sourcing of drugs and solid cost control while also paying down debt, its stock could return to double-digit territory in the next year or two.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.