Making some optimizations and adding extra comments.