Made changes for R>=3