function buyTokensWithWei(address beneficiary)
        internal
    {
        uint256 weiAmount = msg.value;
        uint256 weiRefund = 0;

        
        uint256 tokens = weiAmount.mul(rate);

        
        if (tokensSold.add(tokens) > crowdsaleCap) {
            tokens = crowdsaleCap.sub(tokensSold);
            weiAmount = tokens.div(rate);

            weiRefund = msg.value.sub(weiAmount);
        }

        
        weiRaised = weiRaised.add(weiAmount);

        tokensSold = tokensSold.add(tokens);
        sendPurchasedTokens(beneficiary, tokens);
        emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

        address(wallet).transfer(weiAmount);
        wallet.splitFunds();

        if (weiRefund > 0) {
            msg.sender.transfer(weiRefund);
        }
    }