Modified .R file for assignment. Needs more testing