low level traverse function, inspired by $.dir