Added a functional version of algorithm.  This algorithm is not good for the delite model.