performance improvements, other general refactoring