Simplify code and add comments