factoring out duplicate code