Server code refactoring
Big refactoring