fixed a bug in the r2 function