Neatened up some of the regex/ matching stuff