Fix a few issues.

Fixes #5011 and #5002 .
I don't really expect the answer to be yes, but could changing working dir cause confusion? Should there be a popd equivalent so nothing changes after the command runs?