with simple doctest added. works only for one phase for now.  need further improvement