Fixing the while loop for directory traversal