Added variable distance threshold