improved r script