Changing code to use EventEmitter