refactored with each_with_object instead of inject because mutation