Add support for custom working directories

Supersedes #822 — I needed to add a commit to make the merge clean.