More efficient function to calculate combinations