refactor out common code