optimized the code, add comments