head function not properly pulling from last n members, n variable being reset on any value, n not being read properly by regex function