Refactored the logic tree code