refactor; add trace depth limit