Improve directory handling