refactored with bag