Move  code into service. Slight code reorganization.