fixed undefined errors with event listeners