refactoring the directory change function to a HoleInTheMiddle pattern