Restructured to avoid code duplication