Added events, fixed a typeof 'undefined' comparison