Added a lot of comments to clarify code