Split out utility functions from main.R