Made the directory check code more efficient

This updates builds off the parent pruning code that was put in yesterday and makes it more efficient.

Thanks to @staabm for pointing out a better way to do this.