add unit test + refactor code