fix some bugs
add some unit tests and fix bugs