function resolveChallenge(uint challengeId, bool decision)
  external
  gameIsNotOver {
    Challenge storage challenge = challenges[challengeId];
    
    require(challenge.resolved == false, "Challenge already resolved.");

    
    if(block.timestamp < (challenge.startDate + challenge.time + daysToResolve))
      require(challenge.mentor == msg.sender, "You are not the mentor for this challenge.");
    else require((challenge.user == msg.sender) || (challenge.mentor == msg.sender), "You are not the user or mentor for this challenge.");

    uint mentorFee;
    uint serviceFee;
    
    address user = challengeToUser[challengeId];
    address mentor = challengeToMentor[challengeId];

    
    challenge.successed = decision;
    challenge.resolved = true;

    uint remainingValue = challenge.value;

    
    if(challenge.mentorFee > 0) {
      serviceFee = challenge.mentorFee.div(100).mul(serviceFeePercentage);
      mentorFee = challenge.mentorFee.div(100).mul(100 - serviceFeePercentage);
    }
    
    if(challenge.mentorFee > 0)
      remainingValue = challenge.value.sub(challenge.mentorFee);

    uint valueToPay;

    if(decision) {
      
      valueToPay = remainingValue;
      
      uint currentBonus = bonusFund[user];
      if(currentBonus > 0)
      {
        uint bonusValue = bonusFund[user].div(100).mul(bonusPercentage);
        if(currentBonus <= minBonus)
          bonusValue = currentBonus;
        bonusFund[user] -= bonusValue;
        emit BonusFundChanged(user, bonusFund[user]);

        valueToPay += bonusValue;
      }
    }
    else {
      bonusFund[user] += remainingValue;
      emit BonusFundChanged(user, bonusFund[user]);
    }

    
    if(valueToPay > 0)
      user.transfer(valueToPay);

    if(mentorFee > 0)
      mentor.transfer(mentorFee);

    if(serviceFee > 0)
      owner().transfer(serviceFee);

    emit ChallengeResolved(challengeId, user, mentor, decision);
  }