Refactor k distance algo structuring