function buyItem(uint256 _narcoId, uint256 _district, uint256 _itemIndex, uint256 _quantity) public payable whenNotPaused{
    require(narcoCore.ownerOf(_narcoId) == msg.sender); 

    uint256 narcoWeedTotal;
    uint256 narcoCokeTotal;
    uint16[6] memory narcoSkills;
    uint8[4] memory narcoConsumables;
    uint16 narcoLevel;

    (
                ,
      narcoWeedTotal,
      narcoCokeTotal,
      narcoSkills,
      narcoConsumables,
                ,
                ,
      narcoLevel,
                ,
                ,
    ) = narcoCore.getNarco(_narcoId);

    require(getNarcoLocation(_narcoId) == uint8(_district)); 
    require(uint8(_quantity) > 0 && districts[_district].isStocked[_itemIndex] == true); 
    require(marketItems[_itemIndex].levelRequired <= narcoLevel || _district==7); 
    require(narcoCore.getRemainingCapacity(_narcoId) >= _quantity || _itemIndex>=6); 

    
    if (_itemIndex>=6) {
      if (marketItems[_itemIndex].skillAffected!=5){
            
            require (marketItems[_itemIndex].levelRequired==0 || narcoSkills[marketItems[_itemIndex].skillAffected]<marketItems[_itemIndex].upgradeAmount);
          }else{
            
            require (narcoSkills[5]<20+marketItems[_itemIndex].upgradeAmount);
      }
    }

    uint256 costPrice = districts[_district].marketPrices[_itemIndex] * _quantity;

    if (_itemIndex ==0 ) {
      costPrice = max(districts[_district].marketPrices[0], (((districts[_district].weedPot / districts[_district].weedAmountHere)/100)*(100+spreadPercent))) * _quantity;
    }
    if (_itemIndex ==1 ) {
      costPrice = max(districts[_district].marketPrices[1], (((districts[_district].cokePot / districts[_district].cokeAmountHere)/100)*(100+spreadPercent))) * _quantity;
    }

    require(msg.value >= costPrice); 
    
    if (_itemIndex > 1 && _itemIndex < 6) {
      
      narcoCore.updateConsumable(_narcoId, _itemIndex - 2, uint8(narcoConsumables[_itemIndex - 2] + _quantity));
       _distributeRevenue(costPrice, _district , 50, 50);
    }

    if (_itemIndex >= 6) {
        
        
        narcoCore.updateSkill(
          _narcoId,
          marketItems[_itemIndex].skillAffected,
          uint16(narcoSkills[marketItems[_itemIndex].skillAffected] + (marketItems[_itemIndex].upgradeAmount * _quantity))
        );
        _distributeRevenue(costPrice, _district , 50, 50);
    }
    if (_itemIndex == 0) {
        
        narcoCore.updateWeedTotal(_narcoId, true,  uint16(_quantity));
        districts[_district].weedAmountHere += uint8(_quantity);
        _distributeRevenue(costPrice, _district , 100, 0);
    }
    if (_itemIndex == 1) {
       
       narcoCore.updateCokeTotal(_narcoId, true, uint16(_quantity));
       districts[_district].cokeAmountHere += uint8(_quantity);
       _distributeRevenue(costPrice, _district , 0, 100);
    }

    
    if (msg.value>costPrice){
        msg.sender.transfer(msg.value-costPrice);
    }

  }