refactoring list traversal and adding get tail operation