Started on a cleanup into a more object oriented and robust implementation.