Refactor server and add simple debug mode