corrected the code and have added the comments at the top