added comments to important places for clarity