Can you please explain how the code works? Maybe add some comments to make it easier to understand.