It seems like the json sometimes came in out of chronological order, i've added explicit checks to the code to compensate for this