avoid calling end more than once