refactoring of accept_loop