refactor server and main