corrected the algorithm to implement properly the noiseless version