Refactor some common code in the emitter