fix closedir segfaults (again)

yay, new segfaults associated with closedir.  I left a couple of comments this time to mark the reason why they were moved.