changed variable names, reordered, added comments to improve readability