refactred build_path with recursion