tweaked the r code