move computations to service

you use a different style for while loops below, try to keep it consistent.