Refactor code to use a type queue instead of performing depth-first processing