refactor server command loop