Refactor to event emitter