corrected algo