modified a few var names and added comments to increase readability