Refactoring searching directories code