Use opendir+chdir to change back to the original directory.

This removes the direct dependency on Cwd module.