pulled out common code to functions