Move cleanup code out of EventEmitter object