Refactored and restructure server code