Implementing the main logic of generating combinations