alternative implementation with combinatorics