Refactored some of a code for Forestry compatibility.