Added some explanatory comments to modified function.