Understanding algorithm