update some code and add some tests.