function buyTokens(address _beneficiary) public payable whenNotPaused {
    validatePurchase();

    uint256 toFund = calculateToFund();
    uint256 toReturn = msg.value.sub(toFund);

    require(toFund > 0);

    uint256 rate = getRate();
    uint256 tokens = rate.mul(toFund);

    require(tokens > 0);

    if (block.timestamp < phase2StartTime) { 
      phase1WeiRaised = phase1WeiRaised.add(toFund);
    } else {
      phase2WeiRaised = phase2WeiRaised.add(toFund);
    }

    if (purchaserFunded[msg.sender] == 0) {
      numPurchasers = numPurchasers.add(1);
    }

    purchaserFunded[msg.sender] = purchaserFunded[msg.sender].add(toFund);
    token.generateTokens(_beneficiary, tokens);

    emit TokenPurchase(msg.sender, _beneficiary, toFund, tokens); 

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

    vault.deposit.value(toFund)(msg.sender);
  }