Initial rewrite of server

+ refactoring