better algorithm to parse path