Updated extractPath function.