Small fix to prevent end event from being emitted twice