massive code cleanup, prep for linking via Rcpp