factoring out some common code