corrected following algorithm