Fixed an edge case where multiple 'end' events could be emitted.