Make event listeners optional and clean up code