Fixed the determination of the start and end nodes