added detailed comments to the code