clever way to determine path

This change seems unrelated to the rest; what is its motivation, and can you make it in a separate commit?