 +						epoll_ctl(efd, EPOLL_CTL_MOD, peer_pty, &edge_peer);
 +						is_edge_mode = 1;
 +					}
-+					printf("EPOLL HUP\n");
 +				}
 +				else
 +				{
-+					close(events[i].data.fd);
++					int found_client = 0;
++					/* If a client closes it needs to be cleaned up */
++					for (p = clients; p; p = next)
++					{
++						next = p->next;
++						if (events[i].data.fd == p->fd)
++						{
++							found_client = 1;
++							client_activity(p);
++							break;
++						}
++					}
++					if (!found_client)
++						close(events[i].data.fd);
 +					if (events[i].data.fd == the_pty.fd)
 +						exit(1);
 +					continue;

 		return 0;
 	}
 
@@ -670,7 +901,7 @@ master_main(char **argv, int waitattach,
 		/* Child - this becomes the master */
 		if (fd[0] != -1)
 			close(fd[0]);

 	}
 }
 
@@ -589,12 +801,43 @@ master_main(char **argv, int waitattach,
 {
 	int fd[2] = {-1, -1};
 	int s;
