Renamed variables and added a few comments to explain unknown functions