Fix bugs in message tracking algorithm.