fixed the traversal algorithm