Restructured code for packaging in R