added endFlag object and modified update function