add unittests and fix some small bugs