Handle dmg_min == 0
Use orddicts for better speed
Start of implementation in C