Updated inline comments to make the logic clearer