added almost all the requisite code - missing discard time