Add test and simplify the logic