edited server loop