move common code out of if/else