How to calculate the amount for 1031 exchange in an owner-occupied 4-plex?

Let's say there's a multi family 4-plex. Owner bought it for 400k and lived one of the units for two years, then sells it for 700k. The owner pays 6% commission to the realtor for the sale which is $42,000. The owner also has a mortgage on it that's for 350k. I want to figure out some numbers. 

Let's assume for simplicity's sake that the owner lived in 25% of it for those two years. 

 

What is the total amount that the owner can claim tax free? 

What is the total amount that needs to go through a 1031 exchange? 

 

Here's how I calculated it so far: 

700k * 0.75 (rental percentage) = 525k sales price for the rental portion. 

700k * 0.25 (primary percentage) = 175k sales price for the primary portion. 

 

Now the purchase price: 

400k * 0.75 = 300k. (rental)

400k * 0.25 = 100k. (home)

 

So, 525k-300k= 225k capital gains? Which means we need to reinvest this 225k via a 1031 exchange? 

I assume 175k-100k = 75k home gains, which can be tax free. 

 

Question 1: Where do we deduct the realtor fees? From the "rental" portion, home portion, or split it 75/25? Or deduct it before we calculate the rental/home portions of the sale price? 

 

Question 2: What happens with the rest of the profit (difference between original purchase price and mortgage left)? We bought it for 400k, but the mortgage is only at 350k right now, so where does that extra 50k profit go? Is it just tax free cash just like the primary residence profit?