factorized main loop into new class