                 if state == .Connected {
                     NSLog("%@", "connected to \(session.hashValue)")
                     // Update the set of peers in the session
-                    peersInSession.insert(peerID)
+                    self.session.peersInSession.insert(peerID)
                     
                     // If currently a guest, stop looking for host
                     stopSearchingForHosts()

    public init(serviceType: String, deviceId: String) {
        self.serviceType = serviceType
        myPeerId = MCPeerID(displayName: deviceId)
        peersInSession = Set<MCPeerID>()
    }
    
    public convenience init(serviceType: String) {

                    delegate?.connectedToPeer(peerID)
                } else {
                    NSLog("%@", "not connected to \(session.hashValue)")
                    NSLog("%@", "peersInSession \(self.session.peersInSession)")
                    
                    // If session.connectedPeers is empty, it implies that either:
                    // - Self is disconnected from the session
                    // - The session has no other connected peer
                    // Combine with peersInSession count > 1, we can be certain that
                    // self is disconnected.
                    if self.session.connectedPeers.isEmpty && self.session.peersInSession.count > 1 {
                        NSLog("%@", "Self was removed")
                        self.session.peersInSession.remove(myPeerId)
                        delegate?.disconnectedFromSession()
                    } else {
                        NSLog("%@", "Self was not removed")
                        NSLog("%@", "Current host: \(self.session.host)")
                        
                        // Assign a new host as the first item in peersInSession set that is not the old host
                        // If the current item is the next in line, convert it to a host
                        if self.session.host == peerID {
                            var nextHost = peerID
                            for peer in self.session.peersInSession {
                                if peer != peerID {
                                    nextHost = peer
                                    break
                                }
                            }
                            NSLog("%@", "Next potential host: \(nextHost)")
                            
                            if myPeerId == nextHost {
                                NSLog("%@", "my peer is the next host")
                                // Convert current item into a host
                                self.session.host = myPeerId
                                NSLog("%@", "New host \(self.session.host?.displayName)")
                                stopSearchingForHosts()
                                startAdvertisingHost()
                            }
                        }
                    }
                }
                
