refactored services
improved algorithm