removed C++, as R code is vectorised.