diff --git a/src/or/cpuworker.c b/src/or/cpuworker.c
index 6b73d9510a6f3348a52b9adfa1174406b81aeefa..4e66a40f49b50fab9f86f43ca16e902755960a44 100644
--- a/src/or/cpuworker.c
+++ b/src/or/cpuworker.c
@@ -268,6 +268,7 @@ static int spawn_cpuworker(void) {
   if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
     log(LOG_ERR, "Couldn't construct socketpair: %s",
         tor_socket_strerror(tor_socket_errno(-1)));
+    tor_cleanup();
     exit(1);
   }
 
diff --git a/src/or/directory.c b/src/or/directory.c
index d391d4a9f79dd4a074b3557723a19d58b75f1852..4ab1b938099b0989e7f714cc07fed3b2fe1c5145 100644
--- a/src/or/directory.c
+++ b/src/or/directory.c
@@ -66,6 +66,8 @@ directory_post_to_dirservers(uint8_t purpose, const char *payload,
 
   for(i=0; i < smartlist_len(rl->routers); i++) {
     router = smartlist_get(rl->routers, i);
+    /* Note: this posts our descriptor to ourselves, if we're an
+     * authdirserver. But I think that's ok. */
     if(router->is_trusted_dir)
       directory_initiate_command(router, purpose, payload, payload_len);
   }
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index ae8968ce2a7c2c8fd72dbcb29331e4c498c8488b..d74d4d0a004373dde8f39556178ab07c4babfa5e 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -670,6 +670,7 @@ size_t dirserv_get_directory(const char **directory)
     /* use a new copy of the dir, since get_dir_from_string scribbles on it */
     if (router_load_routerlist_from_directory(new_directory, get_identity_key())) {
       log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
+      tor_cleanup();
       exit(0);
     }
     free(new_directory);
diff --git a/src/or/dns.c b/src/or/dns.c
index 3e16b6b643ffa317b366df441f6589aa0cc7459f..01b5bd46eeb7062ccae7e19ffbe835ad980e350d 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -691,6 +691,7 @@ static int spawn_dnsworker(void) {
   if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
     log(LOG_ERR, "Couldn't construct socketpair: %s",
         tor_socket_strerror(tor_socket_errno(-1)));
+    tor_cleanup();
     exit(1);
   }
 
diff --git a/src/or/main.c b/src/or/main.c
index 9844150b485c1642430a2b0e1b89594d92fc3aac..ffeeae5cfda1c39548b10ccdc35165eb4c9b820a 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -496,6 +496,7 @@ static void run_scheduled_events(time_t now) {
    */
   if(shutting_down && shutting_down <= now) {
     log(LOG_NOTICE,"Clean shutdown finished. Exiting.");
+    tor_cleanup();
     exit(0);
   }
 
@@ -722,11 +723,13 @@ static int do_hup(void) {
   /* first, reload config variables, in case they've changed */
   /* no need to provide argc/v, they've been cached inside init_from_config */
   if (init_from_config(0, NULL) < 0) {
+    tor_cleanup();
     exit(1);
   }
   /* reload keys as needed for rendezvous services. */
   if (rend_service_load_keys()<0) {
     log_fn(LOG_ERR,"Error reloading rendezvous service keys");
+    tor_cleanup();
     exit(1);
   }
   if(retry_all_listeners() < 0) {
@@ -815,10 +818,12 @@ static int do_main_loop(void) {
     if(please_shutdown) {
       if(!server_mode()) { /* do it now */
         log(LOG_NOTICE,"Interrupt: exiting cleanly.");
+        tor_cleanup();
         exit(0);
       }
       if(shutting_down) { /* we've already been asked. do it now. */
         log(LOG_NOTICE,"Second sigint received; exiting now.");
+        tor_cleanup();
         exit(0);
       } else {
         log(LOG_NOTICE,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", SHUTDOWN_WAIT_LENGTH);
@@ -885,6 +890,7 @@ static void catch(int the_signal) {
 //    case SIGABRT:
     case SIGTERM:
       log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
+      tor_cleanup();
       exit(0);
     case SIGINT:
       please_shutdown = 1;
@@ -966,7 +972,7 @@ static void dumpstats(int severity) {
 /** Called before we make any calls to network-related functions.
  * (Some operating systems require their network libraries to be
  * initialized.) */
-int network_init(void)
+static int network_init(void)
 {
 #ifdef MS_WINDOWS
   /* This silly exercise is necessary before windows will allow gethostbyname to work.
@@ -987,10 +993,7 @@ int network_init(void)
  */
 void exit_function(void)
 {
-  /* Remove our pid file. We don't care if there was an error when we
-   * unlink, nothing we could do about it anyways. */
-  if(options.PidFile)
-    unlink(options.PidFile);
+/* XXX if we ever daemonize, this gets called immediately */
 #ifdef MS_WINDOWS
   WSACleanup();
 #endif
@@ -998,7 +1001,7 @@ void exit_function(void)
 
 /** Main entry point for the Tor command-line client.
  */
-int tor_init(int argc, char *argv[]) {
+static int tor_init(int argc, char *argv[]) {
 
   /* give it somewhere to log to initially */
   add_temp_log();
@@ -1046,7 +1049,12 @@ int tor_init(int argc, char *argv[]) {
   return 0;
 }
 
+/** Do whatever cleanup is necessary before shutting Tor down. */
 void tor_cleanup(void) {
+  /* Remove our pid file. We don't care if there was an error when we
+   * unlink, nothing we could do about it anyways. */
+  if(options.PidFile)
+    unlink(options.PidFile);
   crypto_global_cleanup();
 }
 
@@ -1061,7 +1069,7 @@ void nt_service_control(DWORD request)
           service_status.dwCurrentState = SERVICE_STOPPED;
           return;
   }
-  SetServiceStatus(hStatus, &service_status);     
+  SetServiceStatus(hStatus, &service_status);
 }
 
 void nt_service_body(int argc, char **argv)
diff --git a/src/or/or.h b/src/or/or.h
index ec1e9eafdad3d6987febbf0c461fb5fdf3d5a70d..8bdfb1f80e2570ebaa3d5478d378b1e25a70649e 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1202,7 +1202,7 @@ int server_mode(void);
 int advertised_server_mode(void);
 int proxy_mode(void);
 
-int main(int argc, char *argv[]);
+void tor_cleanup(void);
 
 /********************************* onion.c ***************************/
 
diff --git a/src/or/rendservice.c b/src/or/rendservice.c
index 76a725a9f9af7ab176bb5433af6a1fe693e6f477..ff25704f3f901222407199b560a6fa31394eccd3 100644
--- a/src/or/rendservice.c
+++ b/src/or/rendservice.c
@@ -24,7 +24,7 @@ typedef struct rend_service_port_config_t {
 #define NUM_INTRO_POINTS 3
 
 /** If we can't build our intro circuits, don't retry for this long. */
-#define INTRO_CIRC_RETRY_PERIOD 60*10
+#define INTRO_CIRC_RETRY_PERIOD 60*5
 /** Don't try to build more than this many circuits before giving up
  * for a while.*/
 #define MAX_INTRO_CIRCS_PER_PERIOD 10
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index 277178f638adafbfc30a568a888f0cca8188ef5c..b0bca530aff06108faf0766bdc5cc365080d04c9 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -302,12 +302,13 @@ int check_software_version_against_directory(const char *directory,
     return -1;
   } else {
     fflush(0);
+    tor_cleanup();
     exit(0);
     return -1; /* never reached */
   }
 }
 
-/** Parse a directory from <b>s</b> and, when done, store the
+/** Parse a directory from <b>str</b> and, when done, store the
  * resulting routerlist in *<b>dest</b>, freeing the old value if necessary.
  * If <b>pkey</b> is provided, we check the directory signature with pkey.
  */
diff --git a/src/or/test.c b/src/or/test.c
index 635ffb15a2f347dd4dba0c2690aba912e7c65986..f76e66d2d87742fb730a9723fffecc2cfffcc19c 100644
--- a/src/or/test.c
+++ b/src/or/test.c
@@ -858,6 +858,8 @@ test_dir_format()
   test_eq(1, is_obsolete_version("0.0.2", "Tor 0.0.2pre1,Tor 0.0.3"));
   test_eq(0, is_obsolete_version("0.1.0", "Tor 0.0.2,Tor 0.0.3"));
   test_eq(0, is_obsolete_version("0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8"));
+  test_eq(0, is_obsolete_version("0.0.5", "0.0.5-cvs"));
+  test_eq(0, is_obsolete_version("0.0.5.1-cvs", "0.0.5"));
 }
 
 void test_rend_fns()