diff --git a/rowbot b/rowbot
index 36ca481..af7c6b5 100755
--- a/rowbot
+++ b/rowbot
@@ -119,9 +119,7 @@ fi
 ###
 
 cleanup() {
-  if [[ -v tls_pid || $tls = no ]]; then
-    exec {in_sock}>&- {out_sock}>&- {log}>&-
-  fi
+  debug "cleaning up rowbot"
 
   if [[ -v tls_pid ]]; then
     kill "$tls_pid"
@@ -129,11 +127,22 @@ cleanup() {
   fi
 
   if [[ -v ping_pid ]]; then
+    debug "cleaning up alarm in %d" "$BASHPID"
     kill "$ping_pid"
   fi
+
+  if [[ -v tls_pid || $tls = no ]]; then
+    exec {in_sock}>&- {out_sock}>&-
+
+    if (( log != 1 )); then
+      {log}>&-
+    fi
+  fi
 }
 
-trap cleanup EXIT
+if (( BASHPID == $$ )); then
+  trap cleanup EXIT
+fi
 
 alarm-handler() {
   ping "row your bot gently down the stream"
@@ -155,7 +164,7 @@ if [[ $reload = yes ]]; then
   trigger=$TRIGGER dev=$DEV level=$LOG_LEVEL
   registered=$REGISTERED
 
-  debug "doing a reload. pid is %d" "$$"
+  debug "doing a reload. pid is %d" "$BASHPID"
 
   if [[ -v KEEP_TRYING ]]; then
     keep_trying=$KEEP_TRYING desired_nick=$DESIRED
@@ -649,7 +658,7 @@ hook_post_PRIVMSG_control_panel() {
       fi
 
       if [[ -v ping_pid ]]; then
-        export PING_pid=$ping_pid
+        export PING_PID=$ping_pid
       fi
 
       privmsg "$to" "reloading..."
@@ -719,7 +728,7 @@ if [[ $reload = yes ]]; then
   privmsg "$RELOAD_TO" done.
 else
   registered=no
-  info "rowbot's pid is %d" "$$"
+  info "rowbot's pid is %d" "$BASHPID"
   nick "$nick"
   user "$ident" "$realname"
 fi