Working version. Refactor dist calling function to avoid bugs and code duplication