Helper R functions