simplify code, add comments