As gas prices continue to soar to historic levels, savvy consumers often search for the best deal to get the most out of their hard-earned cash.

But sometimes, finding the best price can come just by crossing a lane or two of traffic.

So why do gas prices vary so much from station to station?

Experts say it comes down to a few key factors: taxes, wholesale prices and profit margins.

So let’s start with taxes. California has notoriously high gasoline taxes, but those vary from county to county and even city to city.

According to NACS, the Association for Convenience and Fuel Retailing, all gas retailers must charge the federal gas excise tax, which is currently around $0.18. States also have their fees and taxes. In California, the tax is as high as $0.86. Alaska has the lowest state taxes on gasoline at about $0.33, according to NACS and the American Petroleum Institute.

A gas station in downtown Los Angeles might be right on the corner where a different city’s boundaries begin. That city could have different taxes on gas.

California also has strict requirements on fuel blends that aren’t present in other states. Gasoline that meets California standards is more expensive to produce and that cost is passed onto the consumer.

So taxes and fuel blend play a role in the variance from state to state and city to city, but it doesn’t entirely explain why gas is priced differently in the same city on opposite sides of the same street.

The main reason for the disparity, experts say, comes down to margins and competition.

Gasoline retailers have to buy their gas from somewhere, and wholesalers have their prices. For gas stations, a large factor in how they determine prices comes down to the brand of gasoline they sell. Chevron is priced differently than Exxon, which is priced differently than Shell. Some are more expensive than others, and the customer pays for the added cost because gas stations don’t make huge profits on each gallon sold. Many consumers are OK with paying more for certain brands due to reputation or manufacturer recommendations.

And the higher volume of gasoline sold means the gas station probably gets a better wholesale price, which makes it easier for them to sell gas at a lower retail price.

Another thing to consider: does the gas station have a convenience store on-site? Most gas stations make small profits from gasoline sales, but those with convenience stores make more than half of their total earnings off of the items sold inside, NCAS says.

Gas stations with stores tend to price their gas lower, knowing they’ll make up the difference in convenience store sales. The lower prices entice people to spend money in the stores where margins are much more profit-friendly for the business owner. So if you buy a large soda and some sunflower seeds, you’re likely helping pay to keep those prices down.

But as in most industries, the biggest reason for the pricing disparity probably comes down to competition.

If a gas station without a convenience store is near one that does, it might be beneficial to adjust those prices accordingly. If a gas station isn’t a name brand business, it’ll have its own set of challenges that are different from the name brand business, including fewer discounts on wholesale gasoline. A store that is inconvenient to drive to might have to lower its prices to entice customers who would typically go to the competitor across the street, which isn’t as much of a pain to get to.

There are numerous reasons why one gas station might sell gas for cheaper than its neighbor. Still, ultimately it comes down to a complicated equation for business owners who want to maximize their profits while testing what the consumer will tolerate and prioritize.