Improved function to read dirs recursively