rprog004 pa2 code, finally working