fix forestry crash, + some refactoring