For nearly two years, refineries in the Midwest have been buying crude oil at steep discounts thanks to a glut of U.S. and Canadian oil. But drivers in the Midwest haven't seen a corresponding decrease in gasoline prices. In fact, they sometimes pay more at the pump than people in other parts of the country, even as windfall profits flow to BP, Koch Industries Inc. and other large Midwestern refiners.

"It's good to be a refiner," said Tom Kloza, chief oil analyst at the Oil Price Information Service, a company that tracks energy markets. "For 20 years, the rule of thumb was that if you made $5 a barrel east of the Rockies, that was a good profit for a refinery. Recently, we saw a period in the Midwest where refiners were making $40, or $50, or even $60 a barrel on gasoline."

The disparity of fortunes between Midwest refiners and consumers isn't a surprise to industry analysts.

In today's complex fuel market, retail gasoline prices are no longer just a reflection of the cost of oil. A host of other factors—such as refinery fires, power outages and damage from extreme weather events—now have an increasing impact, in part because there are fewer refineries fulfilling the nation's thirst for fuel.

The correlation between the price of oil and the price of gasoline has become so skewed that analysts say it calls into question one of the central benefits touted by supporters of the controversial Keystone XL pipeline: That the project would lower U.S. gas prices by providing Gulf Coast refiners with a steady flow of cheaper oil from Canada.

"Now there are all sorts of wild cards—all sorts of additional complexities," said Patrick DeHaan, senior petroleum analyst at GasBuddy.com, which helps drivers find low gas prices. "Sometimes it doesn't matter if you're putting $100 oil into that refinery or $60 Canadian oil, because we're becoming more subject to these refining limitations and outages."

Nowhere has this shift been more obvious than in the Midwest, where ramped up tar sands oil production and construction of new and expanded pipelines helped create an oil glut that first surfaced in 2011.

Although Midwest pump prices have periodically dipped a bit, refiners that bought the cheaper crude kept most of the gains for themselves. Earnings reports show they processed the discounted oil, then sold the resulting fuel to distributors and consumers at prices that were pushed up by Midwest fuel shortages and by the higher cost of other kinds of crude oil.

In mid-December, for instance, when the discount peaked, refiners could buy Canadian heavy oil for about $44 per barrel—almost half the going price of $88.32 per barrel for West Texas Intermediate, the U.S. benchmark oil. Gas prices in the Midwest sank to the year's lowest average, $3.14 a gallon. But that was still 10 cents more than drivers were paying on the Gulf Coast and only 11 cents cheaper than the national average, according to figures from the U.S. Energy Information Administration.

So far this year, retail gas prices in the Midwest have been mixed. They were below the national average until the end of April, but in late May, additional refinery problems sent them soaring to 20 cents above the national average. The price spike dissipated after some of the problems were solved. But industry-watchers expect the gyrations at the pump to continue.

"We've had crazy up and we've had crazy down in the Midwest," said Kloza, of the energy price tracking company. "Everything we've seen so far in 2013 in the Midwest has had nothing to do with crude. It's had nothing to do with [uncertainty around the] Keystone."

Complex Fuel Market

The biggest factor in the region's gas prices has been the partial shutdown of the Midwest's largest refinery—BP's plant in Whiting, Ind.—so it can be upgraded to process larger amounts of Canada's heavy crude. The project is taking longer than planned, and analysts expect it to be at least a few more months before the refinery runs at full capacity.

Several other Midwest refineries have also had problems. A maintenance project in Joliet, Ill. took almost twice as long as planned. There was a fire in Detroit. Lightning struck a plant in Lemont, Ill. There were pipeline spills in Wisconsin.

"The Midwest has just had a lot of bad luck," said Phil Flynn, a Chicago-based energy trader at the Price Futures Group. "We also had power outages during a storm that slowed production. We had that pipeline in Minnesota, because of the floods, knocked out of commission."

Because of those glitches, gasoline inventories in the Midwest fell close to 25-year lows.

The region's refiners aren't complaining, though. Even though Canada's heavy crude has gotten a lot more expensive in 2013—it's now trading at $82.34 per barrel compared to $96.50 per barrel for U.S. benchmark oil—refiners that buy it still pocket a double-digit discount that doesn't get passed along in lower gasoline prices.

The strongest profit margins have been in the Midwest, but refiners elsewhere also benefited from snags in gasoline supplies that sent pump prices soaring well above production costs. Refinery problems on the West Coast last year triggered California's fastest and largest spike in gasoline prices. When Hurricane Sandy hit the Northeast, it led to gas lines, fuel shortages and higher gas prices that lasted long after normal fuel flows were restored.

The Energy Information Administration estimated that despite cheaper oil imports from Canada, higher domestic production and lower gasoline demand, Americans spent almost 4 percent of their household income on gas in 2012. That's the highest percentage in nearly 30 years, and puts it in a tie with 2008, when oil prices neared $150 a barrel and gasoline prices set record-highs that were recently eclipsed in many regions.

Oil and fuel markets have grown more complex in other ways, too. Energy traders now play a significant role in setting market prices, and so do things like currency exchange rates and the ebb and flow of fuel exports. In addition, every gallon of U.S. gasoline contains an increasing amount of ethanol, making corn prices an increasingly important element in determining what consumers pay at the pump.

What's more, surprise developments—such as the recent surge in U.S. oil production—have disrupted longstanding price relationships between different types of crude as well as between oil and gasoline. At times, U.S. crude prices and gasoline prices have even moved in opposite directions.

The price of "crude is still crucial, but ... gasoline is ultimately what we need to burn," said Kloza of the Oil Price Information Service. For that reason, refinery glitches and other events that put a crimp in gasoline supplies have made U.S. fuel prices "much, much more bipolar than crude."

Refiners Are in Charge

Refinery hiccups have been around as long as refineries themselves. But the ripple effects of a single outage are more immediate and more dramatic now because of an accelerating trend toward fewer, larger, and more complicated refineries.

In addition, refiners and others in the industry now keep as little extra oil, gasoline and diesel on hand as possible. Crude oil gets delivered to refiners essentially on an as-needed basis. Gasoline and diesel are carried from refineries to gas stations in a similar just-in-time fashion, whether the fuel is being moved across town or from the Gulf Coast to the Northeast.

Gasoline stockpiles in the Rocky Mountain region, for example, amounted to 24.9 gallons per person last year, a 42 percent drop from 43 gallons per person in 1980, according to the Oil Price Information Service. The change is even more striking on the West Coast, where gasoline inventories that stood at 38.5 gallons a person in 1980 fell by almost half, to 19.8 gallons per person, last summer.

Low inventories help refiners cut operating costs and reflect a distribution system that's become more efficient over the years. But they also can lead to magnified and immediate price spikes at the pump if there's a problem in the supply chain.

Another constantly shifting element that affects pump prices is the lure of higher profits for refiners that export fuel—especially diesel—from the United States to countries where demand is growing. The focus on exports has accelerated so much that the U.S. recently became a net exporter of fuel for the first time. Because diesel exports are particularly lucrative, many Gulf Coast refiners are making more diesel and less gasoline from each barrel of oil.

Everything in Flux

All these changes are still transforming the industry, making it harder than ever to make assumptions about energy prices.

Just a few years ago, it was possible to reliably predict the rough cost of the nation's gasoline merely by observing the price of benchmark crude on the New York Mercantile Exchange, where traders buy and sell contracts for North Sea Brent and West Texas Intermediate crudes based on actual conditions and speculation about oil prices, supply and demand. The connection between oil and gasoline prices was so ingrained and so consistent that politicians, the oil industry and the public have equated the two for decades.

Drawing on that well-established link, it made sense to assume that building the Keystone XL would directly affect gasoline prices. After all, the project would boost the nation's imports of cheap crude from Canada, and more oil would translate into lower fuel prices.

In this new world, however, it's just not that simple, analysts say.

Although it's hard to predict whether all these trends will continue to hold such sway over gasoline prices, they say the dominant and unshakable correlation between oil and gasoline prices has been severely weakened, if not broken altogether.

"Crude oil versus your prices for refined products ... I think they're separate and distinct entities now," said Andre van der Valk, who worked at an oil company and now owns four gas stations in Southern California.

Still, many politicians cling to the notion that importing more of Canada's tar sands oil through the Keystone would lower U.S. gas prices. The project's effect on gas prices is expected to be one of the factors that the Obama administration considers when determining whether the pipeline is in the national interest.

No matter which way the decision goes, refiners expect their good fortune to continue. If the Keystone isn't built, the supply of heavy Canadian crude won't be as robust for Gulf Coast refiners, but it will continue to flow via rail and other pipelines. If the Keystone is built, their profits will get an extra boost. They'll be able to buy even more of the cheaper Canadian crudes, along with crude from North Dakota and West Texas, and then sell the byproducts at prices that don't reflect the oil discount.

"Right now, if you're a refiner, whether you're on the Gulf Coast, the East Coast or whatever, you're saying to your fellow refiners, 'Pinch me, I want to make sure this is not a dream,'" Kloza said.