more refactoring in order to identify longest distance chain