optimize directory traversal method