refactored the combinations code to reduce duplication