Distance Transform refactored