fix TTL overflow and some refactorings