Made changes to algorithm

The algorithm should now be correct but I did not read it very well. The output is poor.