Factor out some helper functions into common library