cardiff: Moving all grouping code into function

As all the code to create the groups based on the hw properties is done,
let's put all this stuff into a single function to make the main() code
easier to read.