refine unit test; a few bugs