function calculateTokens(uint _amount, uint _stage, uint _stageAmount) public returns (uint) {
    bonuses[1] = firstBonusPercent;
    bonuses[2] = secondBonusPercent;
    bonuses[3] = thirdBonusPercent;
    bonuses[4] = 0;
    
    amounts[1] = 0;
    amounts[2] = 0;
    amounts[3] = 0;
    amounts[4] = 0;
    
    int amount = int(_amount);
    
    uint i = _stage;
    while (amount > 0) {
      if (i > 3) {
        amounts[i] = uint(amount);
        break;
      }
      if (amount - int(_stageAmount) > 0) {
        amounts[i] = _stageAmount;
        amount -= int(_stageAmount);
        i++;
      } else {
        amounts[i] = uint(amount);
        break;
      }
    }
    
    uint tokens = 0;
    uint bonusTokens = 0;
    uint _tokens = 0;
    for (i = _stage; i <= 4; i++) {
      if (amounts[i] == 0) {
        break;
      }
      _tokens = rate.mul(amounts[i]).div(1 ether);
      bonusTokens = _tokens * bonuses[i] / 100;
      tokens += _tokens + bonusTokens;
    }
    
    return tokens;
  }