fixed distance calculations
started thresholding code