Add latestid and some refactoring.