fix TTL overflow and some refactorings
Merge pull request #2305 from eminn/fix/TTLOverflow-master

fix TTL overflow and some refactorings