add unit test and simplify