Owning a home is exciting, challenging and the biggest investment of many people’s lives. It’s also a good way to reduce your tax bill.

Home-related tax breaks begin as soon as you close on your new abode and last throughout your time in the house. But to maximize them, you need to follow some rules.

American homeowners own a variety of types of homes — and the federal Internal Revenue Code recognizes this.

When it comes to tax breaks, your home can be a house, a condominium, a co-op apartment, a mobile home or even a recreational vehicle or boat. As long as it has sleeping, cooking and bathroom facilities, the IRS will allow you to claim several home-related expenses.

Once you own a home, you’ll probably have to change your tax-filing habits. Taking full tax advantage of a property requires most people to itemize instead of using the standard deduction. This means filling out the longer Form 1040 and accompanying Schedule A, where you detail your home-related tax-deductible expenses. Most homeowners find itemizing worth the effort, and plenty of tax software takes the pain out of the process.

» MORE: Itemizing vs. standard deduction

There are two key sections of Schedule A that deal with home deductions:

Many homeowners’ biggest tax break comes via their monthly mortgage payment, a large part of which goes toward loan interest. As long as your home loan is $1 million or less, all of that interest paid is tax deductible. And if you paid discount points to get a lower loan rate, you usually can deduct those points from your taxes, too.

If you’ve taken out a home equity loan or line of credit, you can generally deduct the interest you paid on that debt. It doesn’t matter if you used the funds on your residence, as long as you received $100,000 or less.

Private mortgage insurance — which you’re likely paying if you didn’t make a 20% down payment — is treated as mortgage interest and thus is deductible, at least for the 2016 tax year. The catch is you can’t deduct it if you have an income greater than $109,000.

In most cases, another portion of your monthly mortgage payment covers your annual property taxes. (Your lender then pays your tax bill.) In others, homeowners pay them directly. Either way, you can deduct these payments as long as you own your residence.

If this is your first year in the house, double check the settlement sheet you received at closing. You probably split the year’s real estate taxes with the seller based on the date of sale. The share you paid will appear on this closing document and it’s fully tax deductible.

» MORE: Try this federal tax calculator

All those years you spend in your home can provide substantial tax savings. The best tax break, however, is likely to come when you sell.

When single taxpayers sell a primary residence, they can pocket up to $250,000 in profit and not owe any capital gains taxes. The allowance doubles for married couples who file a joint return.

Note that this tax-exclusion amount is based on your profit, not your sales price. You could sell your home for $1 million and still not owe Uncle Sam as long as your profit is no more than $250,000 or, if married, $500,000.

Profit from a home sale is calculated as it is with any other capital asset you sell: Subtract your home’s adjusted basis from the price you got.

For most homeowners, the basis equals the original purchase price plus the value of all capital improvements made to the property. These improvements must add to your home’s value and don’t include routine maintenance and repairs.

Keep track of upgrades to your property. The greater your home’s basis, the lower your sale profit, ideally to a tax-free level. (And even if you do make some taxable profit on your home sale, at least it will be taxed at the lower long-term capital gains tax rate.)

With such a great tax savings, you might be tempted to become a serial home seller. That could work, but you must meet some requirements in order to qualify for the tax-free home-sale profit.

Most folks meet these requirements. So in addition to packing your personal belongings onto a moving van, you pack away a nice tax-free check into your bank account.

The article How Owning or Selling a Home Affects Your Taxes originally appeared on NerdWallet.