EDT: Complete algorithm, untested of course