simplify code + add unit test