refactor into handlers
foreach working, fixed tree traversal algorithm