Add more unittests and some code simplification.