Fixed and Finalised R-code