Taxes wont apply when you try to sell tokens on Pancakeswap

Hello,

I have a transfer override function to add fees - taxes when users swap my token.
My issue is that the taxes work when the user is swapping BNB for Token, but when user tries to Swap Token for BNB they wont. What am i missing?

function transfer(address recipient, uint256 amount) public override returns (bool) {
        
        if (_msgSender() == uniswapPair) {
            
            // If wallets are excluded from fees dont pay taxes for swap
            if (excludedFromFees[_msgSender()] || excludedFromFees[recipient]) {
                _transfer(_msgSender(), recipient, amount);
                return true;
            }

            // Stops investors from owning more than 2% of the total supply from purchasing YMT from PancakeSwap.
            if (!excludedFromFees[_msgSender()] && !excludedFromFees[recipient]) {
                require((balanceOf(recipient) + amount) < (totalSupply() / 75), "You can't have more than 2% of the total YMT supply after a PancakeSwap swap.");
            }
        
        }
        
        if (_msgSender() != uniswapPair) {
                        
            // No fees from wallet to wallet transactions but check for 2% of recipient
            if (!excludedFromFees[_msgSender()] && !excludedFromFees[recipient]) {
                require((balanceOf(recipient) + amount) < (totalSupply() / 75), "You can't have more than 2% of the total YMT supply.");    
                _transfer(_msgSender(), recipient, amount);
                return true;
            }

            // No fees for sending from or to contract wallets
            if (excludedFromFees[_msgSender()] || excludedFromFees[recipient]) {
                _transfer(_msgSender(), recipient, amount);
                return true;
            }                                               
        }
// Yacht transaction fee.
        uint256 yachtFee = (amount * yachtTransactionFeePercent) / 100;
        // Marketing team transaction fee.
        uint256 marketingFee = (amount * marketingFeePercent) / 100;
        // Developer team transaction fee.
        uint256 developerFee = (amount * developerFeePercent) / 100;
        // Burn fee
        uint256 burnFee = (amount * burnFeePercent) / 100;

        // The total fee to send to the contract address.
        uint256 totalFee = yachtFee + marketingFee + developerFee + burnFee;
 
        // Sends the transaction fees to the contract address
        _transfer(_msgSender(), address(this), totalFee);
         
        // Sends [initial amount] - [fees] to the recipient
        uint256 valueAfterFees = amount - totalFee;

        _transfer(_msgSender(), recipient, valueAfterFees);
        return true;
    }

after some researching i understood should apply this login under transferFrom or _transfer?

Can you please give me an example to implement above logic under transferFrom or _transfer?

I have managed to compile with transferFrom and emit Transfer but i get error "Fail with error 'ds-math-sub-underflow"

This was resolved like this:

function _transfer(address sender, address recipient, uint256 amount) internal virtual override {
              
        if((sender == uniswapPair || recipient == uniswapPair) && !excludedFromFees[sender] && !excludedFromFees[recipient]) {

            // Yacht transaction fee.
            uint256 yachtFee = (amount * yachtTransactionFeePercent) / 100;
            // Marketing team transaction fee.
            uint256 marketingFee = (amount * marketingFeePercent) / 100;
            // Developer team transaction fee.
            uint256 developerFee = (amount * developerFeePercent) / 100;
            // Burn fee
            uint256 burnFee = (amount * burnFeePercent) / 100;

            // The total fee to send to the contract address.
            uint256 totalFee = yachtFee + marketingFee + developerFee + burnFee;
    
            // Sends the transaction fees to the contract address
            super._transfer(sender, address(this), totalFee);
            
            // Prepares amount afterfees
            amount -= totalFee;
        }

Thanks @Amxx https://github.com/OpenZeppelin/openzeppelin-contracts/issues/3093#issuecomment-1008329227

**Update

Integrated 2% and added pinksale antibot

// Internal Transfer function override to collect taxes only on Swap    
    function _transfer(address sender, address recipient, uint256 amount) internal virtual override {

        //when to collect taxes      
        if((sender == uniswapPair || recipient == uniswapPair) && !excludedFromFees[sender] && !excludedFromFees[recipient]) {
            
            // Only use PinkAntiBot if this state is true
            if (antiBotEnabled) {
            pinkAntiBot.onPreTransferCheck(sender, recipient, amount);
            }

            //Investor cannot have more than 2% of total supply
            if(sender == uniswapPair && !excludedFromFees[sender] && !excludedFromFees[recipient]) {
                require((balanceOf(recipient) + amount) < (totalSupply() / 76), "You can't have more than 2% of the total supply.");                                
            }

            // Yacht transaction fee.
            uint256 yachtFee = (amount * yachtTransactionFeePercent) / 100;
            // Marketing team transaction fee.
            uint256 marketingFee = (amount * marketingFeePercent) / 100;
            // Developer team transaction fee.
            uint256 developerFee = (amount * developerFeePercent) / 100;
            // Burn fee
            uint256 burnFee = (amount * burnFeePercent) / 100;

            // The total fee to send to the contract address.
            uint256 totalFee = yachtFee + marketingFee + developerFee + burnFee;
    
            // Sends the transaction fees to the contract address
            super._transfer(sender, address(this), totalFee);
            
            // Prepares amount afterfees
            amount -= totalFee;
        }

        super._transfer(sender, recipient, amount);
    }

Let me know what you think of this