Everyone would like to claim they are engaged in an activity that results in a profit that is non-taxable. Here is one activity that has the potential of generating tax-free income – but if not handled correctly, what you thought would be tax-free money can be subject to hefty taxes.

The activity? Buy a home with the intent of flipping but that you treat as your principal residence. Then renovate and sell it, and begin the whole process all over. If you satisfy all the tax requirements, you can continuously exclude up to $500,000 of each gain if married filing jointly ($250,000 for other taxpayers).

What rules do you need to adhere?

1. The home must be your principal residence for at least two out of the past five years before you sell;

2. You must satisfy the look-back rule, which means the two-year residency period cannot overlap the two-year residency period of a former principal residence;

3. You didn’t acquire the home through a like-kind exchange during the past five years, also known as a 1031 exchange (If it was acquired that way more than five years ago, you still may be subject to other rules coverings its time as a rental property.);

4. You cannot be subject to expatriate tax;

5. You must not fail one of the eligibility tests. These are listed in IRS Publication 523, but the main ones are:

- You separate or divorce from your spouse during the ownership period; or

- Your spouse died during the ownership period and you don’t meet the two-year ownership rule

If you satisfy all the requirements, up to $250,000 ($500,000 if married filing jointly) of the gain from the sale of your renovated principal residence is excluded from federal income tax.

Better yet, you can start the process over with a new home and continue to reap tax-free capital gains. The tax break is per property, not for your lifetime.

However, keep sufficient documentation to show that you have followed the rules. For example, your principle residence is your main home, and you can only have one main home at time. If you own or live in more than one home, the IRS will look at a range of facts and circumstances, the most important being where you spend most of your time. Other factors the IRS may consider include the home address you list for your mail, your voter registration, your address for your federal and state income tax returns (this is an easy check for the IRS) and your driver’s license and vehicle registration.

Moreover, it is not just the address of where you live, it is where you go to work, do your banking, the residence of family members and other places you go regularly for entertainment, community activities and clubs to socialize. It will also be helpful to show that despite significant renovations that you may doing at your principal residence such as gutting rooms, you still have the ability to live in that house.

Read about the housing market:Home-builder confidence sees steepest slide since 2014 as housing headwinds catch up

What happens if you fail to satisfy the principal residence exclusion rules? Then the entire gain from the sale is subject to capital-gains tax. If the house has been held for less than a year, any gains are considered short term and are taxed at your marginal tax rate, which could be as high as 37%.

If the house has been owned for more than one year, the gains are treated as long-term capital gains and are taxed at preferential rates of 0%, 15% or 20%, depending on your tax bracket. That is, a person in the 10% or 12% tax bracket generally enjoys the 0% capital gain tax rate. Likewise, a person in the 37% tax bracket generally are subject to the 20% capital gain tax rate. (Note that the crossover points for the capital-gains tax rates don’t match exactly with income-tax rates.)

And if you decide to continue buying and renovating homes (better known as house flipping) and don’t treat any of those as your primary residence? While the gain you realize from the sale of the renovated home may be treated as capital gains, it more likely will be treated as ordinary income.

If you flip a home within one year and the gain is treated as capital gains, it is a short-term gain and is taxed at your marginal tax rate, which could be as high as 37%.

Read:The biggest lessons we learned from flipping a house

But if you are buying, renovating and selling with more frequency, you will likely be deemed a “real estate dealer”. As a “real estate dealer”, you are a business and therefore, the gain, regardless of the holding period, is taxed as ordinary income, like any other business.

What determines whether you are a dealer? There is no an easy answer. Treasury rules state that in general, someone engaged in the business of selling real estate to customers with a view to the possible gains and profits is a real-estate dealer. On the other hand, an individual who merely holds real estate for investment or speculation and receives rental income from them isn’t a dealer.

A review of tax court cases suggests activity level, the turnover rate and personal conduct are signs of a person’s intent. Overall, it will probably be the facts and circumstances that will determine if you are a dealer or not. It will be hard for the IRS to overlook the activity if there are a bunch of Form 1099-S — Proceeds from Real Estate Transactions — popping on your annual income tax returns. So if you look like a business, act like a business, you are probably a business. And now that you are a business, the profits made on the sale of the home is treated as ordinary income that may be subject to the self-employment tax of 15.3% (applicable to earnings up to $132,900 in 2019).

Therefore, the ideal strategy is to buy, renovate and sell your principal residence after living in it for at least two years and then start the process over again.

But if you make a mistake in qualifying for the transaction, you may be paying capital-gains tax or, more likely, ordinary income tax plus self-employment tax (ouch!). So consult a qualified tax person to minimize the likelihood of going astray of the law.

Anthony P. Curatola is the Joseph F. Ford Professor of Accounting at Drexel University’s LeBow College of Business and editor of the Taxes column for “Strategic Finance”.