Changed the logic of endTime calculation