generalize process_path() for any number of path elements