fix bugs and add ut tests.