Airfares are highly variable. You could be sitting next to someone who paid several hundreds more than you to be on the same plane even if you’re flying to the same destination.

What drives these price differences? Let’s break down the big factors.

Supply & Demand

Airlines respond to the same simple economics as any seller. They want passengers to pay as much as they’re willing for their seats.

“They charge what the market will bear, and hopefully that’s more than their costs,” said Robert W. Mann, an airline industry consultant and former airline executive.

Unlike many businesses, airlines know certain passengers are willing to pay more than others.

Airlines also have the ability to charge different prices to different customers, thanks to sophisticated algorithms that help them ensure they fill — and sometimes overfill — each flight. On top of that, airlines aren’t selling just one product.

Say you buy a ticket from New York to Chicago. Many of the people on the plane with you may have bought tickets for different journeys, from New York to San Francisco or Hawaii or beyond. Airlines have to balance the number of seats they offer to someone making the shorter trip to Chicago against the number of seats they offer to people making longer trips who are often willing to pay much more for their seats, Mann explained.

“What you’re willing to sell and what you’re willing to show as available for sale depends on the network you’re selling,” he said.

Airlines think in terms of selling an entire network of trips, not just individual seats, Mann said. This can make setting prices complicated. But that doesn’t mean there aren’t factors that have a discreet impact on your airfare.

Location

The airport a passenger chooses matters a lot, said Patrick Surry, chief data scientist for Hopper, an app that calculates the best times to book flights.

Airfares from airports that have multiple airlines competing over the same routes will often result in better deals.

Conversely, airfare might be higher at smaller airports or even bigger hubs dominated by a single airline because of a lack of competition.

Government fees also play a role. For example, the United Kingdom charges an Air Passenger Duty that can inflate the price of premium cabin seats flying out of Heathrow by upwards of $200.

Time

Airfares can vary widely by when they’re purchased. In general, night departures go for less than morning departures, Mann said.

Passengers can also save money by purchasing tickets far in advance of their trips, although the cheapest tickets are restricted, or non-refundable. Fares typically stay at their lowest until a few weeks before departure, at which point airlines typically increase prices until takeoff, Mann said.

However, that’s not always the case. If a flight is booking slowly, airlines may release more low-priced tickets, Mann said.

Location also affects how prices change over time, Surry said. Destinations that draw lots of business travelers who tend to book late and are willing to pay higher prices for unrestricted tickets will see airfares rise the most as takeoff approaches.

More touristy destinations with fewer business travelers, like Hawaii or Cancun, won’t see prices rise as much.

The airlines’ goal is to make the most money from a limited number of seats, knowing that the people willing to pay the most won’t book until days before the flight leaves. They have to fill up the plane enough to cover fuel and other costs while leaving seats to sell to last-minute buyers.

How to Get the Cheapest Airfare

Airlines have tons of data available that allow them to determine what to charge each customer.

“We’re trying to fight fire with fire,” said Surry. “We also have an army of computers.”

While price changes can sometimes seem random, there are certain predictable elements, Surry said. Prices are generally comparable during the same seasons each year, and they rise in a predictable way as departure approaches.

However, the best broad tip he had was for passengers to be flexible in their travel dates and destinations. A few years ago, he traveled to Europe with his family and found that he could pay much less by flying to Lisbon instead of London and Paris while still being able to visit via cheaper intracontinental flights or trains.

Booking vacations can be complicated because travelers have to account for not only airfare, but also hotel rates and possibly car-rental prices, Mann said. Make sure to take all your costs into account when planning your trip.

Travelers can also save on their airfare if they use the right credit card. Many airlines offer co-branded credit cards that award points and miles for purchases. Just remember, many travel rewards cards require excellent credit, so be sure to check your free score on Credit.com to see whether you qualify.

This article originally appeared on Credit.com.