function buyTokens() issetTokensForSale saleIsOn payable {   
        uint tokens = msg.value.mul(1 ether).div(rate);
        if(tokens > 0)   {
             address referer = 0x0;
            
             uint bonusTokens = 0;
            if(now < start.add(7* 1 days)) {
    			bonusTokens = tokens.mul(45).div(100); 
            } else if(now >= start.add(7 * 1 days) && now < start.add(14 * 1 days)) { 
    			bonusTokens = tokens.mul(40).div(100); 
            } else if(now >= start.add(14* 1 days) && now < start.add(21 * 1 days)) { 
    			bonusTokens = tokens.mul(35).div(100); 
            } else if(now >= start.add(21* 1 days) && now < start.add(28 * 1 days)) { 
    			bonusTokens = tokens.mul(30).div(100); 
            } 
            tokens = tokens.add(bonusTokens);
            
    		
    		
    	
                if(msg.data.length == 20) {
                  referer = bytesToAddress(bytes(msg.data));
                  require(referer != msg.sender);
                  uint refererTokens = tokens.mul(refererPercent).div(100);
                }
    	
    		
    		
    		if(availableTokensforPreICO > countOfSaleTokens.add(tokens)) {  
    			token.transfer(msg.sender, tokens);
    			currentPreICObalance = currentPreICObalance.add(msg.value); 
    			countOfSaleTokens = countOfSaleTokens.add(tokens); 
    			balances[msg.sender] = balances[msg.sender].add(msg.value);
    			if(availableTokensforPreICO > countOfSaleTokens.add(tokens).add(refererTokens)){
    			     
    			     if(referer !=0x0 && refererTokens >0){
    			        token.transfer(referer, refererTokens);
    			        	countOfSaleTokens = countOfSaleTokens.add(refererTokens); 
    			     }
    			}
    		} else {
    			
    			msg.sender.transfer(msg.value);
    		}
        }else{
            
           msg.sender.transfer(msg.value);
        }
    }