Order discount is influencing checkout extension line cost total Amount

Okay i have a function that applies order discount that function basically number of items in the cart that after discounts are above X amount. So if i have 3 items that after product level discounts are after X the discount wold be 90$ (30$. 3).
Problem is that the total CART LINE amount is now getting lowered (so if i have exact 500$ product it will fall below 500$ ) this would result in discount recalculating (30$
* 2), but now that problematic cart line is above 500$ which would change the discount again and the total price would change infinitely.

Why is the discount taken into account, and how to prevent it?