implemented draft of the algorithm. more functions need to be
implemented. not tested