Refactored most custom functions to R subdirectory as part of ongoing cleanup effort