add unit test, refactor