improve/fix cpu-hogging regex, tidy up code