added comments to code for clarity