miscellaneous helper functions written in R