rprog004 pa2 code, commented final version