Moved the trees to a separate function and other small edits