I cannot follow the logic of this part. Could you add more comments explaining the algorithm?