Refactored to avoid needless copying of function pointers.
Added functional algorithm (still has kinks.)