Bring the algorithm description in accordance with actual implementation. Also provide some comments to the code.