priv->ignore_next_arrange_event = FALSE;
        priv->char_input_queue = g_async_queue_new();
        priv->line_input_queue = g_async_queue_new();
+       /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */
        priv->resource_lock = g_mutex_new();
        priv->resource_loaded = g_cond_new();
        priv->resource_info_available = g_cond_new();
-       /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */
+       priv->image_cache = NULL;
        priv->interrupt_handler = NULL;
     priv->root_window = NULL;
     priv->fileref_list = NULL;
        g_cond_free(priv->resource_info_available);
        g_mutex_unlock(priv->resource_lock);
        g_mutex_free(priv->resource_lock);
+       g_slist_foreach(priv->image_cache, (GFunc)clear_image_cache, NULL);
+       g_slist_free(priv->image_cache);
        /* Unref input queues (this should destroy them since any Glk thread has stopped by now */
        g_async_queue_unref(priv->char_input_queue);
        g_async_queue_unref(priv->line_input_queue);
 
        g_mutex_unlock(glk_data->resource_lock);
 
        /* Store the image in the cache */
+       gdk_threads_enter();
+
        if( g_slist_length(glk_data->image_cache) >= IMAGE_CACHE_MAX_NUM ) {
-               printf("Cache size exceeded\n");
                struct image_info *head = (struct image_info*) glk_data->image_cache->data;
                gdk_pixbuf_unref(head->pixbuf);
                g_free(head);
                glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache);
        }
-       printf("Loading pixbuf\n");
        info->pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
+       gdk_pixbuf_ref(info->pixbuf);
        info->width = gdk_pixbuf_get_width(info->pixbuf);
        info->height = gdk_pixbuf_get_height(info->pixbuf);
-       printf("Caching pixbuf\n");
        glk_data->image_cache = g_slist_prepend(glk_data->image_cache, info);
 
+       gdk_threads_leave();
+
        g_object_unref(loader);
        return info;
 }
 static struct image_info*
 image_cache_find(struct image_info* to_find)
 {
-       printf("Finding image %d\n", to_find->resource_number);
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
        GSList *link = glk_data->image_cache;
 
+       gdk_threads_enter();
+
        /* Empty cache */
        if(link == NULL) {
-               printf("Cache is empty\n");
+               gdk_threads_leave();
+               printf("Cache miss for image %d\n", to_find->resource_number);
                return NULL;
        }
 
        /* Iterate over the cache to find the correct image and size */
        do {
                struct image_info *info = (struct image_info*) link->data;
-               printf("Examining cache entry %d\n", info->resource_number);
                if(info->resource_number == to_find->resource_number) {
                        /* Check size: are we looking for a scaled version or the original one? */
                        if(to_find->scaled) {
                                if(info->width >= to_find->width && info->height >= to_find->height) {
+                                       gdk_threads_leave();
+                                       printf("Cache hit for image %d\n", to_find->resource_number);
                                        return info; /* Found a good enough match */
                                }
                        } else {
                                if(!info->scaled) {
-                                       printf("Cache hit\n");
+                                       gdk_threads_leave();
+                                       printf("Cache hit for image %d\n", to_find->resource_number);
                                        return info; /* Found a match */
                                }
                        }
                }
        } while( (link = g_slist_next(link)) );
 
+       gdk_threads_leave();
+
+       printf("Cache miss for image %d\n", to_find->resource_number);
        return NULL; /* No match found */
 }
 
 glui32
 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
 {
-       printf("get_info(%d)\n", image);
        struct image_info *to_find = g_new0(struct image_info, 1);
        struct image_info *found;
        to_find->resource_number = image;
        to_find->scaled = FALSE; /* we want the original image size */
 
        if( !(found = image_cache_find(to_find)) ) {
-               printf("Cache miss for %d\n", image);
                found = load_image_in_cache(image, 0, 0);
                if(found == NULL)
                        return FALSE;
-       } else {
-               printf("Cache hit for %d\n", image);
        }
 
        if(width != NULL)
 glui32
 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
 {
-       printf("image_draw(%d)\n", image);
        VALID_WINDOW(win, return FALSE);
        g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
 
                return FALSE;
        }
 
-       printf("image info: %d x %d, scaled=%d, pixbufaddr=%d\n", info->width, info->height, (int)info->scaled, (int)info->pixbuf);
-       gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
+       gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, GDK_PIXBUF((GdkPixbuf*)info->pixbuf), 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
 
        /* Update the screen */
        gtk_widget_queue_draw(win->widget);