function refund(uint8 lotteryId) public {
        
        
        require (state[lotteryId] == State.Running);
        require (block.timestamp > (started[lotteryId] + lifetime[lotteryId]));
        require (ticketsSold[lotteryId] < maxTickets[lotteryId]);
        
        
        
        
        require(msg.sender == owner  || playerInfoMappings[lotteryId][msg.sender].changedOn > started[lotteryId]);
        
        uint256 notSend = 0;
        
        
        
        state[lotteryId] = State.Refund; 
        
        for (uint16 i = 0; i < maxTickets[lotteryId]; i++) {
            
            address tOwner = ticketsAllocator[lotteryId][i];
            
            if (tOwner != address(0)) {
                uint256 value = playerInfoMappings[lotteryId][tOwner].ticketsCount*ticketPrice[lotteryId];
                    
                bool sendResult = tOwner.send(value);
                if (!sendResult) {
                    LostPayment(tOwner,value);
                    notSend += value;
                }
            }
        }
        
        
        if (notSend > 0) {
            owner.send(notSend);
        }
        
        
        clearState(lotteryId);
    }