Added some comments for the logic.