Add sanity checks and fix broken directory contents handling