Made a few changes to the code to get consistent rank values. Added comments to make the code more clear to understand later.