Refactored to match upstream