Algorithm corrected