big refactoring to make the server code more pythonic