attempt at implementing Algorithm W in the AG