Fixed bad algorithms.  First working version.