Repackage internal code into functions. (readability). Made cntr a local var as it was interfering with a trace