Change readdir to readdir_r for thread safety