Fix the output formatting to file. And fixed distance algorithm.