Added a bunch of comments to explain the code