Refactoring the path find method