Fix bug in path building logic