Added more comments to make the code clearer in certain places.