refactored main logic to a service