Refactored R-scripts