refined code and added comments to each section of code