Reverted distance map algorithm to the faster previous version