added comments to clarify code