Algorithm modified