algo modified only for master