Refactor and improve sandpit directory handling.
Tighten up test utility functions.