Fixed tree-based calculation so it now works for loops with simple exits