Commenting added, variable names made more pedagogic.