refactor with unit testing