added tests, repaired some bugs