diff --git a/lib/modules/Protocols.pmod/X.pmod/Types.pmod b/lib/modules/Protocols.pmod/X.pmod/Types.pmod
index 4a99298f1b53526d48ba75aff2b92da0f68ff273..aee87ade667f086dbfac1a8cc3476b6ece57055a 100644
--- a/lib/modules/Protocols.pmod/X.pmod/Types.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/Types.pmod
@@ -206,6 +206,8 @@ class Drawable
 {
   inherit XResource;
 
+  object colormap;
+
   object CreateGC_req()
   {
     object req = Requests.CreateGC();
@@ -243,7 +245,7 @@ class Drawable
     object req = CreatePixmap_req(width, height, depth);
     display->send_request(req);
     
-    return Pixmap(display, req->pid, this_object());
+    return Pixmap(display, req->pid, this_object(),colormap);
   }
 
   object FillPoly_req(int gc, int shape, int coordMode, array(object) p)
@@ -315,7 +317,7 @@ class Drawable
   {
     object req = PutImage_req(gc, depth, tx, ty, width, height, data);
     req->format = format;
-    display->send_request(req);
+    display->send_request( req );
   }
 
   object ImageText8_req(int gc, int x, int y, string str)
@@ -368,13 +370,14 @@ class Pixmap
   {
     ::create( @args );
     if(sizeof(args)>2 && objectp(args[2]))  parent = args[2];
+    if(sizeof(args)>3 && objectp(args[3]))  colormap = args[3];
   }
 }
 
 class Window
 {
   inherit Drawable;
-  object visual, colortable, parent;
+  object visual, parent;
   int currentInputMask;
 
   mapping(string:array(function)) event_callbacks = ([ ]);
@@ -567,22 +570,24 @@ class Window
   {
     object req = CreateWindow_req(x, y, width, height,
 				  border_width,depth,visual);
+    object c = colormap||parent->defaultColorMap;
 
     if (attributes)
+    {
       req->attributes = attributes;
-
+      if(attributes->Colormap) c= attributes->Colormap;
+    }    
 
     // object w = Window(display, req->wid);
     display->send_request(req);
     object w = new(display, req->wid, visual, this_object());
-    w->colortable = req->attributes->Colormap;
+    w->colormap = c;
     w->currentInputMask = req->attributes->EventMask;
     return w;
   }
     
   object CreateSimpleWindow(int x, int y, int width, int height,
-			    int border_width,
-			    int border, int background)
+			    int border_width,int border, int background)
   {
     object req = CreateWindow_req(x, y, width, height,
 				  border_width,0,0);
diff --git a/lib/modules/Protocols.pmod/X.pmod/XImage.pmod b/lib/modules/Protocols.pmod/X.pmod/XImage.pmod
index e039f2b94f4c3c133499b6073f9c8c0038ab8a04..55bd38049e0c07633248c581a8b29f89fde2e892 100644
--- a/lib/modules/Protocols.pmod/X.pmod/XImage.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/XImage.pmod
@@ -6,6 +6,7 @@
 
 // Base class.
 // FIXME: Why not inherit Image.Image directly?
+// Guess five times... /Per
 class Image_wrapper
 {
   class funcall
@@ -35,19 +36,19 @@ class Image_wrapper
     // Changed by the inheriting class.
   }
 
-  void copy(mixed ... args)
+  object copy(mixed ... args)
   {
     return image->copy( @args );
   }
 
-  varargs object paste(object what, int x, int y)
+  object paste(object what, int|void x, int|void y)
   {
     image->paste(what, x, y);
     clear_caches(x,y,what->xsize(),what->ysize());
     redraw(x,y,what->xsize(),what->ysize());
   }
 
-  varargs object paste_mask(object what, object mask, int x, int y)
+  object paste_mask(object what, object mask, int|void x, int|void y)
   {
     image->paste_mask(what, mask, x, y);
     clear_caches(x,y,what->xsize(),what->ysize());
@@ -87,7 +88,7 @@ class XImage
 
   int depth, bpp;
   function converter;
-  int linepad;
+  int linepad, swapbytes;
   int rmask, gmask, bmask;
 
   int offset_x, offset_y;
@@ -120,7 +121,7 @@ class XImage
 
     int max_pixel;
 
-    Array.map((array)wanted,
+    Array.map(((array)wanted+({({255,255,255}),({0,0,0})})),
 	    lambda(array o){
 	      return colormap->AllocColor(o[0]*257,o[1]*257,o[2]*257);
 	    });
@@ -133,8 +134,6 @@ class XImage
 
     foreach(values(colormap->alloced), mapping m)
       if(m) res[ m->pixel ] = ({ m->red/257, m->green/257, m->blue/257 });
-// What to do with unallocated colors?
-    res = replace( res, 0, ({ 255,0,255 }) );
 
     object ct = Image.colortable( res );
     ct->cubicles(12, 12, 12);
@@ -153,10 +152,12 @@ class XImage
 
   void redraw(int x, int y, int width, int height)
   {
-    werror(sprintf("XImage->redraw: %d, %d, %d, %d, %d, %d\n",
-		   x, y, offset_x, offset_y, width, height));
+    int max_pixels = ((window->display->maxRequestSize - 64)*32) / bpp;
+    int slice = (max_pixels / width)-1;
+
+//     werror(sprintf("XImage->redraw: [%d, %d]+[%d, %d] (%d)\n",
+// 		   x, y, width, height, slice));
 
-    int slice = (window->display->maxRequestSize/depth)*32 / width;
     if(x+width > image->xsize())
       width = image->xsize()-x;
     if (x<0)
@@ -181,24 +182,27 @@ class XImage
       height -= slice;
       if(height < 0) slice += height;
       object mimg = image->copy(x,y,x+width-1,y+slice-1);
-
       if(rmask)
+      {
+	string data = 
+	  converter(mimg,bpp,linepad,swapbytes,rmask,gmask,bmask,
+		    @(ccol?({ccol}):({})));
+
 	window->PutImage( dgc, depth, x + offset_x, y + offset_y,
-			  width, slice,
-			  converter( mimg, bpp, linepad, rmask, gmask, bmask,
-				     @(ccol?({ccol}):({}))));
+			  width, slice, data, 2 );
+      }
       else
       {
 	if(!ccol) ccol = allocate_colortable();
 	string data = converter( mimg, bpp, linepad, depth, ccol );
 	window->PutImage( dgc, bpp, x + offset_x, y + offset_y,
-			  width, slice, data );
+			  width, slice, data, 2 );
       }
       y += slice;
     }
   }
 
-  void set_window(object w)
+  void set_drawable(object w)
   {
     window = w;
     if(!w->visual)
@@ -219,42 +223,54 @@ class XImage
 
       if(!visual)
 	visual = root->visual;
-    } else
+      root = w->root;
+    } else {
+      root = w->parent;
+      while(root->parent) root = root->parent;
       visual = w->visual;
-
-    if(root->visual == visual)
-    {
-      werror("using default visual\n");
-      colormap = root->defaultColorMap;
     }
+    if(w->colormap)
+      colormap = w->colormap;
     else
-      colormap = w->CreateColormap( visual, 0 );
+      colormap = root->defaultColorMap;
+    
+    swapbytes = !w->display->imageByteOrder;
     rmask = visual->redMask;
     gmask = visual->greenMask;
     bmask = visual->blueMask;
     depth = visual->depth;
-    bpp = visual->bitsPerRGB;
-    linepad = 32; // FIXME!
+
+    foreach(w->display->formats, mapping format)
+      if(format->depth == depth)
+      {
+// 	werror(sprintf("found matching format: %O", format));
+	bpp = format->bitsPerPixel;
+	linepad = format->scanLinePad;
+	break;
+      }
     
     switch(_Xlib.visual_classes[visual->c_class])
     {
-     case "StaticGray":  
+     case "StaticGray":
        ccol = Image.colortable(0,0,0, ({0,0,0}), ({255,255,255}), 1<<depth);
        converter = Image.X.encode_pseudocolor;
        break;
-     case "GrayScale":   
-       ccol = Image.colortable(0,0,0, ({0,0,0}), ({255,255,255}), 1<<depth);
-       converter = Image.X.encode_pseudocolor;
-       break;
-     case "PseudoColor": 
+//        ccol = Image.colortable(0,0,0, ({0,0,0}), ({255,255,255}), 1<<depth);
+//        converter = Image.X.encode_pseudocolor;
+//        break;
+     case "GrayScale":
+     case "PseudoColor":
        ccol = 0;
        converter = Image.X.encode_pseudocolor;
        break;
-     case "StaticColor": 
+     case "StaticColor":
      case "TrueColor":
-       if(depth < 16)
+       if(depth <= 16)
        {
-	 ccol = Image.colortable(1<<depth, 1<<rmask, 1<<gmask, 1<<bmask);
+#define BITS(Y) (Image.X.examine_mask(Y)[0])
+	 ccol = Image.colortable(1<<BITS(rmask),
+				 1<<BITS(gmask),
+				 1<<BITS(bmask));
 	 ccol->ordered();
        }
        converter = Image.X.encode_truecolor_masks;
@@ -281,14 +297,21 @@ class WindowImage
 
   void exposed(mixed event)
   {
-    werror("expose...");
+//     werror(sprintf("%O\n", event));
+#ifdef BUGGY_X
+    if(!event->count)
+    {
+      remove_call_out(redraw);
+      call_out(redraw,0.1,0,0,image->xsize(),image->ysize());
+    }
+#else
     redraw(event->x, event->y, event->width, event->height);
-    werror("done\n");
+#endif
   }
 
-  void create(object (Types.Window) w)
+  void create(object(Types.Window) w)
   {
-    set_window(w);
+    set_drawable(w);
     w->set_event_callback("Expose", exposed); // internal callback...
     w->SelectInput("Exposure");
   }
@@ -296,21 +319,10 @@ class WindowImage
 
 class PixmapImage
 {
-  inherit Image_wrapper;
-  object pixmap;
-  object (Types.Window) window, root;
-  object (Types.Visual) visual;
-  object (Types.Colormap) colormap;
-
-  int depth;
-  function converter;
-  int linepad;
-  int rmask, gmask, bmask;
-
+  inherit XImage;
 
-  void create(object (Types.Pixmap) p, object (Types.Window) w)
+  void create(object (Types.Pixmap) p)
   {
-    pixmap = p;
-    window = w;
+    set_drawable( p );
   }
 }
diff --git a/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod b/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod
index 3fc864d923b9caea1cba1b35dd4ef4a6e9628b4b..267ea9f92cf1b10a595a2d0813cf5175def3c621 100644
--- a/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod
@@ -4,7 +4,7 @@
 
 #include "error.h"
 
-#define DEBUG
+// #define DEBUG
 
 constant XPORT = 6000;
 
@@ -235,6 +235,8 @@ class Display
   
   void default_error_handler(object me, mapping e)
   {
+    if(e->failed_request)
+      e->failed_request = e->failed_request[0..32];
     error(sprintf("Xlib: Unhandled error %O\n", e));
   }
 
@@ -340,7 +342,7 @@ class Display
 		int wid = struct->get_uint(4);
 		object r = Types.RootWindow(this_object(), wid);
 		int cm = struct->get_uint(4);
-		r->defaultColorMap = Types.Colormap(this_object(), cm, 0);
+		r->colormap = r->defaultColorMap = Types.Colormap(this_object(), cm, 0);
 		r->whitePixel = struct->get_uint(4);
 		r->blackPixel = struct->get_uint(4);
 		r->currentInputMask = struct->get_uint(4);
@@ -666,14 +668,13 @@ class Display
 
   void close_callback(mixed id)
   {
-    werror("Xlib.close_callback\n");
     if (state == STATE_WAIT_CONNECT)
       connect_handler(this_object(), 0);
     else
       if (close_handler)
 	close_handler(this_object());
       else
-	io_error_handler(this_object());
+	exit(0);
     close();
   }
   
@@ -810,7 +811,7 @@ class Display
     send(data);
 #ifdef DEBUG
     debug_requests[sequence_number & 0x7fff] = data;
-#endif DEBUG
+#endif
     return sequence_number++ & 0x7fff;
   }