Refactor takeDirectory