fix some bugs
add some tests.