add tree prior looping, extensive cleanup
add tree prior looping, extensive cleanup