Index: owl.h
===================================================================
--- owl.h	(revision 1895c292a4af5f8fb2b9305e3ea30bebe2ec9b4e)
+++ owl.h	(revision 40c665796431e6bd112feb7e36e6520b696396c4)
@@ -380,12 +380,4 @@
 } owl_popwin;
 
-typedef struct _owl_popexec {
-  int refcount;
-  owl_viewwin *vwin;
-  int winactive;
-  int pid;			/* or 0 if it has terminated */
-  int rfd;  
-} owl_popexec;
-
 typedef struct _owl_messagelist {
   owl_list list;
@@ -532,4 +524,12 @@
   void *data;
 } owl_dispatch;
+
+typedef struct _owl_popexec {
+  int refcount;
+  owl_viewwin *vwin;
+  int winactive;
+  int pid;			/* or 0 if it has terminated */
+  owl_dispatch dispatch;
+} owl_popexec;
 
 typedef struct _owl_global {
Index: popexec.c
===================================================================
--- popexec.c	(revision 49a8434e3707923940236017e5739cdc2c4d2b7a)
+++ popexec.c	(revision 40c665796431e6bd112feb7e36e6520b696396c4)
@@ -51,13 +51,13 @@
     return NULL;
   } else if (pid != 0) {
+    close(child_write_fd);
     /* still in owl */
-    int muxhandle;
     pe->pid=pid;
     pe->winactive=1;
-    pe->rfd = parent_read_fd;
-    close(child_write_fd);
-    muxhandle = owl_muxevents_add(owl_global_get_muxevents(&g),
-    				  parent_read_fd, OWL_MUX_READ|OWL_MUX_EXCEPT,
-    				  owl_popexec_inputhandler, (void*)pe);
+    pe->dispatch.fd = parent_read_fd;
+    pe->dispatch.cfunc = owl_popexec_inputhandler;
+    pe->dispatch.destroy = owl_popexec_free_dispatch;
+    pe->dispatch.data = pe;
+    owl_select_add_dispatch(&pe->dispatch);
     pe->refcount++;
   } else {
@@ -85,7 +85,7 @@
 }
 
-void owl_popexec_inputhandler(int handle, int fd, int eventmask, void *data)
+void owl_popexec_inputhandler(owl_dispatch *d)
 {
-  owl_popexec *pe = (owl_popexec*)data;
+  owl_popexec *pe = d->data;
   int navail, bread, rv_navail;
   char *buf;
@@ -96,5 +96,5 @@
   /* If pe->winactive is 0 then the vwin has closed. 
    * If pe->pid is 0 then the child has already been reaped.
-   * if pe->rfd is -1 then the fd has been closed out.
+   * if d->fd is -1 then the fd has been closed out.
    * Under these cases we want to get to a state where:
    *   - data read until end if child running
@@ -105,12 +105,10 @@
 
   /* the viewwin has closed */
-  if (pe->rfd<0 && !pe->pid && !pe->winactive) {
-    owl_muxevents_remove(owl_global_get_muxevents(&g), handle);
-    owl_function_debugmsg("unref of %p from input handler at A", pe);
-    owl_popexec_unref(pe);
+  if (!pe->pid && !pe->winactive) {
+    owl_select_remove_dispatch(d->fd);
     return;
   }
 
-  if (0 != (rv_navail = ioctl(fd, FIONREAD, (void*)&navail))) {
+  if (0 != (rv_navail = ioctl(d->fd, FIONREAD, (void*)&navail))) {
     owl_function_debugmsg("ioctl error");
   }
@@ -125,15 +123,9 @@
       owl_viewwin_redisplay(pe->vwin, 1);
     }
-    if (pe->rfd>0) {
-      close(pe->rfd);
-      pe->rfd = -1;
-    }
-    owl_muxevents_remove(owl_global_get_muxevents(&g), handle);
-    owl_function_debugmsg("unref of %p from input handler at B", pe);
-    owl_popexec_unref(pe);
+    owl_select_remove_dispatch(d->fd);
     return;
   }
 
-  if (pe->rfd<0 || !pe->pid || !pe->winactive || rv_navail) {
+  if (d->fd<0 || !pe->pid || !pe->winactive || rv_navail) {
     owl_function_error("popexec should not have reached this point");
     return;
@@ -143,6 +135,6 @@
   if (navail>1024) { navail = 1024; }
   buf = owl_malloc(navail+1);
-  owl_function_debugmsg("about to read %d\n", navail);
-  bread = read(fd, buf, navail);
+  owl_function_debugmsg("about to read %d", navail);
+  bread = read(d->fd, buf, navail);
   if (bread<0) {
     perror("read");
@@ -152,5 +144,5 @@
     buf[navail] = '\0';
   }
-  owl_function_debugmsg("got data:  <%s>\n", buf);
+  owl_function_debugmsg("got data:  <%s>", buf);
   if (pe->winactive) {
     owl_viewwin_append_text(pe->vwin, buf);
@@ -161,4 +153,11 @@
 }
 
+void owl_popexec_free_dispatch(owl_dispatch *d)
+{
+  owl_popexec *pe = d->data;
+  close(d->fd);
+  owl_popexec_unref(pe);
+}
+
 void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
 {
@@ -167,7 +166,6 @@
 
   pe->winactive = 0;
-  if (pe->rfd>0) {
-    close(pe->rfd);
-    pe->rfd = -1;
+  if (pe->dispatch.fd>0) {
+    owl_select_remove_dispatch(pe->dispatch.fd);
   }
   if (pe->pid) {
