refactored to pull out the service logic from the main.