function buy() public payable {
        require(getNow()  > saleStartTime());
        if (getNow()  > saleEndTime()
          && (softCapReached == false
          || token.isWhiteListed(msg.sender) == false)) {

            
            emit Stage(block.number,10);
            require(msg.value == 0);
            emit Stage(block.number,11);
            uint256 amountToReturn = ethPayed[msg.sender];
            totalTokensToTransfer=totalTokensToTransfer-tokensToTransfer[msg.sender];
            tokensToTransfer[msg.sender] = 0;
            ethPayed[msg.sender] = 0;
            softCapReached = totalTokensToTransfer >= softCapInTokens();
            emit Stage(block.number,12);
            msg.sender.transfer(amountToReturn);
            emit Stage(block.number,13);

        }
        if (getNow()  > saleEndTime()
          && softCapReached == true
          && token.isWhiteListed(msg.sender)) {

            emit Stage(block.number,20);
            
            require(msg.value == 0);
            emit Stage(block.number,21);
            uint256 amountToSend = tokensToTransfer[msg.sender];
            tokensToTransfer[msg.sender] = 0;
            ethPayed[msg.sender] = 0;
            require(token.transfer(msg.sender, amountToSend));
            emit Stage(block.number,22);

        }
        if (getNow()  <= saleEndTime() && getNow()  > saleStartTime()) {
            emit Stage(block.number,30);
            ethPayed[msg.sender] = ethPayed[msg.sender] + msg.value;
            tokensToTransfer[msg.sender] = tokensToTransfer[msg.sender] + getCurrentPrice() * msg.value;
            totalTokensToTransfer = totalTokensToTransfer + getCurrentPrice() * msg.value;

            if (totalTokensToTransfer >= hardCapInTokens()) {
                
                emit Stage(block.number,31);
                revert();
                emit Stage(block.number,32);
            }
        }
        if(tokensToTransfer[msg.sender] > 0 &&  token.isWhiteListed(msg.sender) && softCapInTokens()==0){
          emit Stage(block.number,40);
          uint256 amountOfTokens = tokensToTransfer[msg.sender] ;
          tokensToTransfer[msg.sender] = 0;
          emit Stage(block.number,41);
          require(token.transfer(msg.sender,amountOfTokens));
          emit Stage(block.number,42);
        }
        if (totalTokensToTransfer >= softCapInTokens()) {
            emit Stage(block.number,50);
            softCapReached = true;
            emit Stage(block.number,51);
        }
        if (getNow()  > withdrawEndTime() && softCapReached == true && msg.sender == owner) {
            emit Stage(block.number,60);
            emit Stage(address(this).balance,60);
            
            beneficiary.transfer(address(this).balance);
            emit Stage(address(this).balance,60);
            emit Stage(block.number,61);
            token.burn();
            emit Stage(block.number,62);
        }

    }