Extract some duplicated code into helper modules