Fix false positives for paths starting with the same prefix.
Add optimization due to @kriskowal.