Some refactoring on the traversing code