factored out redundant code.