abstracted out traverse function from print