added r optimization