Help with nested functions -- Solidity

Hello, first off I’d like to say that I’m a noob in Solidity so this issue might sound so trivial, pls bear with me. I’m running into a compiler error on line 61 (Stack too deep). I suppose an easy fix would be to break blocks #2 and #3 into smaller functions but still be able to return the results on #4 for the parent function (#1). Basically, I’m would like to call the ‘buy’ function and then return #4 while breaking blocks #2 and #3 into smaller functions as a workaround “Stack too deep” error. How do I achieve this? Any help would be appreciated.

 // Main function  (#1)
    function buy(address baseToken, address token, address exchangeRouterAddress) public payable returns (uint256, uint256, uint256, uint256, uint256, uint256) {
        uint256[] memory gas = new uint256[](2);

        IUniswapV2Router02 exchangeRouter = IUniswapV2Router02(exchangeRouterAddress);

        IERC20 _token = IERC20(token);
        IERC20 _baseToken = IERC20(baseToken);
        address[] memory path = new address[](2);

        if(baseToken != exchangeRouter.WETH()) {
            path[0] = exchangeRouter.WETH();
            path[1] = baseToken;
            exchangeRouter.swapExactETHForTokensSupportingFeeOnTransferTokens{value: msg.value}(0, path, address(this), block.timestamp + 20);
        }
        else {
            IWETH(baseToken).deposit{value: msg.value}();
        }
        uint256 amount = _baseToken.balanceOf(address(this));
        
        // Nest function 1    (#2)
        path = new address[](2);
        path[0] = baseToken;
        path[1] = token;
        uint256 expectedToken = exchangeRouter.getAmountsOut(amount, path)[1];
        _baseToken.approve(exchangeRouterAddress, type(uint256).max);
        uint256 startGas = gasleft();
        exchangeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(amount, 0, path, address(this), block.timestamp + 20);
        gas[0] = startGas - gasleft();
        uint256 receivedToken = _token.balanceOf(address(this));

        // Nest function 2    (#3)
        path = new address[](2);
        path[0] = token;
        path[1] = baseToken;
        uint256 expectedBaseToken = exchangeRouter.getAmountsOut(receivedToken, path)[1];
        _token.approve(exchangeRouterAddress, type(uint256).max);
        startGas = gasleft();
        exchangeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(receivedToken, 0, path, address(this), block.timestamp + 20);
        gas[1] = startGas - gasleft();
        uint256 receivedBaseToken = _baseToken.balanceOf(address(this));

        // Finally return all results at once    (#4)
        return (expectedToken, receivedToken, gas[0], expectedBaseToken, receivedBaseToken, gas[1]);
    }