STERLING, N.D. (AP) — The grain elevator that towers over this small prairie town has been humming with workers loading crops onto rail cars destined for domestic and worldwide markets.

It's a welcome sight in Sterling, North Dakota, and across the upper Great Plains, where in the past two years grain elevators overflowed and mountains of wheat, corn, soybeans and other crops lay in piles, awaiting rail cars that seemed to never come.

Just in time for what the U.S. Agriculture Department expects to be near-record corn and soybean harvests, grain train cars are in abundance. The reversal is attributed to unprecedented spending on track upgrades, political pressure from politicians in agriculture-rich states and a drastic decrease in trains hauling crude and freight to and from western North Dakota's oil-producing region.

"There had been some uncertainties but it has gradually gotten better," said Josh Mardikian, grain manager at the South Central Grain Cooperative elevator in Sterling. As he spoke, a train was being loaded with 24 million pounds of spring wheat, much of which would be used for a national pizza chain's dough. Grain trains had been running late by as much as 45 days in the past 18 months, Mardikian said, but in the past week, mile-long, 110-car trains had shown up some four days earlier than expected.

Oil activity in North Dakota was partly to blame for the widespread shortage and backlog of rail cars from North Dakota and Montana down to Kansas and east into Illinois, National Farmers Union President Roger Johnson said. "It doesn't take too much more on the tracks to really get things snarled up," he said.

The long delays added to costs for grain elevators and agricultural producers, but whether the costs trickled down to consumers depends on the food product manufacturer, said Ed Usset, a grain marketing economist at the University of Minnesota.

"It's conceivable that every bakery in the world could have tried to pass those costs along but oftentimes companies just ride it out until things get better," he said.

Frustrations with grain shipping made it all the way to Washington, where politicians last year called on the Surface Transportation Board to press the railroads, which it oversees, for plans to address the backlog.

BNSF Railway Co. said it has invested billions of dollars in upgrades across its rail network since 2013, including $1 billion in North Dakota alone. The railroad is now shipping all freight "faster, more predictable and more consistent," according to John Miller, a vice president who oversees the Fort Worth, Texas-based company's grain-related business.

"We put the pressure on the railroads and were willing to be a pain to them but we are also willing to give them credit that they've owned up to this problem and appear to have fixed it," said U.S. Rep. Kevin Cramer, a Republican from North Dakota who was one of the politicians who pressed for solutions. "We'll see."

Lochiel Edwards, a grain farmer who represents Montana's Grain Growers Association on rail issues, said the "only logical solution was to build a bigger rail network."

Also contributing to the turnaround is a slowdown in drilling activity in western North Dakota's oil patch due to depressed oil prices. While the state's oil production has remained stable at about 1.2 million barrels a month, the percentage of crude shipped by rail has dropped below 50 percent due to increased refining capacity and additional pipelines.

"I hope we don't have to put up with the crap we had to in the past," Minnesota Farmers Union President Doug Peterson said, especially in a year when his state is seeing bumper crops.

Johnson, the national farmers' union leader, remains optimistic.

"Almost no one is talking about it," he said. "That's the best indicator things have eased up."