Mostly refactoring to move common functions to utility libraries