Modifying to add comments to understand logic of "solve"