Simplify logic and add comments