fixed the r2 function