Correctly handle cyclic directory structures