Starting to cleanup and abstract  code