Index: libhb/hb.c
===================================================================
--- libhb/hb.c	(revision 3168)
+++ libhb/hb.c	(working copy)
@@ -3,6 +3,8 @@
 
 struct hb_handle_s
 {
+    int            id;
+    
     /* The "Check for update" thread */
     int            build;
     char           version[32];
@@ -49,6 +51,7 @@
 
 hb_lock_t *hb_avcodec_lock;
 hb_work_object_t * hb_objects = NULL;
+int hb_instance_counter = 0;
 int hb_process_initialized = 0;
 
 static void thread_func( void * );
@@ -95,16 +98,16 @@
  */
 hb_handle_t * hb_init( int verbose, int update_check )
 {
-	if (!hb_process_initialized)
-	{
+    if (!hb_process_initialized)
+    {
 #ifdef USE_PTHREAD
 #if defined( _WIN32 ) || defined( __MINGW32__ )
-		pthread_win32_process_attach_np();
+        pthread_win32_process_attach_np();
 #endif
 #endif
-		hb_process_initialized =1;
-	}
-	
+        hb_process_initialized =1;
+    }
+    
     hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
     uint64_t      date;
 
@@ -112,7 +115,9 @@
     global_verbosity_level = verbose;
     if( verbose )
         putenv( "HB_DEBUG=1" );
-
+    
+    h->id = hb_instance_counter++;
+    
     /* Check for an update on the website if asked to */
     h->build = -1;
 
@@ -194,7 +199,7 @@
 #ifdef __APPLE__
 	hb_register( &hb_encca_aac );
 #endif
-
+    
     return h;
 }
 
@@ -216,6 +221,8 @@
         putenv( "HB_DEBUG=1" );
     }
 
+    h->id = hb_instance_counter++;
+
     /* Check for an update on the website if asked to */
     h->build = -1;
 
@@ -356,7 +363,7 @@
     struct dirent * entry;
 
     memset( dirname, 0, 1024 );
-    hb_get_tempory_directory( h, dirname );
+    hb_get_temporary_directory( dirname );
     dir = opendir( dirname );
     if (dir == NULL) return;
 
@@ -370,7 +377,7 @@
         for( i = 0; i < count; i++ )
         {
             title = hb_list_item( h->list_title, i );
-            len = snprintf( filename, 1024, "%" PRIxPTR, (intptr_t) title );
+            len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
             if (strncmp(entry->d_name, filename, len) == 0)
             {
                 snprintf( filename, 1024, "%s/%s", dirname, entry->d_name );
@@ -461,8 +468,8 @@
 
     memset( filename, 0, 1024 );
 
-    hb_get_tempory_filename( h, filename, "%" PRIxPTR "%d",
-                             (intptr_t) title, picture );
+    hb_get_tempory_filename( h, filename, "%d_%d_%d",
+                             h->id, title->index, picture );
 
     file = fopen( filename, "rb" );
     if( !file )
@@ -1313,7 +1320,10 @@
     hb_handle_t * h = *_h;
     hb_title_t * title;
 
+    hb_remove_previews( h );
+    
     h->die = 1;
+    
     hb_thread_close( &h->main_thread );
 
     while( ( title = hb_list_item( h->list_title, 0 ) ) )
@@ -1331,9 +1341,41 @@
     hb_list_close( &h->jobs );
     hb_lock_close( &h->state_lock );
     hb_lock_close( &h->pause_lock );
+
     free( h );
     *_h = NULL;
+}
 
+/**
+ * Cleans up libhb at a process level. Call before the app closes. Removes preview directory.
+ */
+void hb_global_close()
+{
+    char dirname[1024];
+    DIR * dir;
+    struct dirent * entry;
+    
+    /* Find and remove temp folder */
+    memset( dirname, 0, 1024 );
+    hb_get_temporary_directory( dirname );
+
+    dir = opendir( dirname );
+    if (dir)
+    {
+        while( ( entry = readdir( dir ) ) )
+        {
+            char filename[1024];
+            if( entry->d_name[0] == '.' )
+            {
+                continue;
+            }
+            memset( filename, 0, 1024 );
+            snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
+            unlink( filename );
+        }
+        closedir( dir );
+        rmdir( dirname );
+    }
 }
 
 /**
@@ -1346,14 +1388,12 @@
 {
     hb_handle_t * h = (hb_handle_t *) _h;
     char dirname[1024];
-    DIR * dir;
-    struct dirent * entry;
 
     h->pid = getpid();
 
     /* Create folder for temporary files */
     memset( dirname, 0, 1024 );
-    hb_get_tempory_directory( h, dirname );
+    hb_get_temporary_directory( dirname );
 
     hb_mkdir( dirname );
 
@@ -1425,25 +1465,6 @@
         hb_stop( h );
         hb_thread_close( &h->work_thread );
     }
-
-    /* Remove temp folder */
-    dir = opendir( dirname );
-    if (dir)
-    {
-        while( ( entry = readdir( dir ) ) )
-        {
-            char filename[1024];
-            if( entry->d_name[0] == '.' )
-            {
-                continue;
-            }
-            memset( filename, 0, 1024 );
-            snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
-            unlink( filename );
-        }
-        closedir( dir );
-        rmdir( dirname );
-    }
 }
 
 /**
@@ -1456,6 +1477,15 @@
 }
 
 /**
+ * Returns the id for the given instance.
+ * @param h Handle to hb_handle_t
+ */
+int hb_get_instance_id( hb_handle_t * h )
+{
+    return h->id;
+}
+
+/**
  * Sets the current state.
  * @param h Handle to hb_handle_t
  * @param s Handle to new hb_state_t
Index: libhb/hb.h
===================================================================
--- libhb/hb.h	(revision 3168)
+++ libhb/hb.h	(working copy)
@@ -99,6 +99,10 @@
    Aborts all current jobs if any, frees memory. */
 void          hb_close( hb_handle_t ** );
 
+/* hb_global_close()
+   Performs final cleanup for the process. */
+void          hb_global_close();
+
 #ifdef __cplusplus
 }
 #endif
Index: libhb/ports.c
===================================================================
--- libhb/ports.c	(revision 3168)
+++ libhb/ports.c	(working copy)
@@ -65,6 +65,7 @@
 #endif
 
 #include <stddef.h>
+#include <unistd.h>
 
 #include "hb.h"
 
@@ -225,9 +226,9 @@
 }
 
 /************************************************************************
- * Get a tempory directory for HB
+ * Get a temporary directory for HB
  ***********************************************************************/
-void hb_get_tempory_directory( hb_handle_t * h, char path[512] )
+void hb_get_temporary_directory( char path[512] )
 {
     char base[512];
 
@@ -251,7 +252,7 @@
     if( base[strlen(base)-1] == '/' )
         base[strlen(base)-1] = '\0';
 
-    snprintf( path, 512, "%s/hb.%d", base, hb_get_pid( h ) );
+    snprintf( path, 512, "%s/hb.%d", base, getpid() );
 }
 
 /************************************************************************
@@ -262,7 +263,7 @@
 {
     va_list args;
 
-    hb_get_tempory_directory( h, name );
+    hb_get_temporary_directory( name );
     strcat( name, "/" );
 
     va_start( args, fmt );
Index: libhb/ports.h
===================================================================
--- libhb/ports.h	(revision 3168)
+++ libhb/ports.h	(working copy)
@@ -30,9 +30,9 @@
 int hb_dvd_region(char *device, int *region_mask);
 
 /************************************************************************
- * Files utils
+ * File utils
  ***********************************************************************/
-void hb_get_tempory_directory( hb_handle_t * h, char path[512] );
+void hb_get_temporary_directory( char path[512] );
 void hb_get_tempory_filename( hb_handle_t *, char name[1024],
                               char * fmt, ... );
 void hb_mkdir( char * name );
Index: libhb/scan.c
===================================================================
--- libhb/scan.c	(revision 3168)
+++ libhb/scan.c	(working copy)
@@ -609,8 +609,8 @@
         
         if( data->store_previews )
         {
-            hb_get_tempory_filename( data->h, filename, "%" PRIxPTR "%d",
-                                     (intptr_t)title, i );
+            hb_get_tempory_filename( data->h, filename, "%d_%d_%d",
+                                     hb_get_instance_id(data->h), title->index, i );
 
             file_preview = fopen( filename, "wb" );
             if( file_preview )
