change a bit the logic after a code review in stack exchange