Add comments and simplify logic