Fix algorithm and prepare for distributed setup.