simplify logic and fix a bug on the way :D