replace getSubpathsAtLength with getSubpath along with some refactoring